mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-12 04:08:14 +02:00
Minor updates based on instruction (#1583)
This contains some minor updates from #1565. --------- Co-authored-by: Marshall Pierce <575695+marshallpierce@users.noreply.github.com>
This commit is contained in:
parent
35442ad424
commit
9563f055e2
@ -42,6 +42,8 @@ struct Package {
|
||||
impl Package {
|
||||
// ANCHOR_END: Package
|
||||
// ANCHOR: as_dependency
|
||||
/// Return a representation of this package as a dependency, for use in
|
||||
/// building other packages.
|
||||
fn as_dependency(&self) -> Dependency {
|
||||
// ANCHOR_END: as_dependency
|
||||
Dependency {
|
||||
|
@ -2,10 +2,10 @@
|
||||
minutes: 10
|
||||
---
|
||||
|
||||
# Lifetimes
|
||||
# Lifetime Annotations
|
||||
|
||||
A reference has a _lifetime_, which must "outlive" the value it refers to. This
|
||||
is verified by the borrow checker.
|
||||
A reference has a _lifetime_, which must not "outlive" the value it refers to.
|
||||
This is verified by the borrow checker.
|
||||
|
||||
The lifetime can be implicit - this is what we have seen so far. Lifetimes can
|
||||
also be explicit: `&'a Point`, `&'document str`. Lifetimes start with `'` and
|
||||
@ -19,7 +19,7 @@ ambiguity; the compiler verifies that there is a valid solution.
|
||||
Lifetimes become more complicated when considering passing values to and
|
||||
returning values from functions.
|
||||
|
||||
```rust,compile_fail
|
||||
```rust,eitable,compile_fail
|
||||
#[derive(Debug)]
|
||||
struct Point(i32, i32);
|
||||
|
||||
|
@ -35,27 +35,29 @@ Recursive data types or data types with dynamic sizes need to use a `Box`:
|
||||
```rust,editable
|
||||
#[derive(Debug)]
|
||||
enum List<T> {
|
||||
Cons(T, Box<List<T>>),
|
||||
/// A non-empty list, consisting of the first element and the rest of the list.
|
||||
Element(T, Box<List<T>>),
|
||||
/// An empty list.
|
||||
Nil,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
|
||||
let list: List<i32> = List::Element(1, Box::new(List::Element(2, Box::new(List::Nil))));
|
||||
println!("{list:?}");
|
||||
}
|
||||
```
|
||||
|
||||
```bob
|
||||
Stack Heap
|
||||
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - - -.
|
||||
: : : :
|
||||
: list : : :
|
||||
: +------+----+----+ : : +------+----+----+ +------+----+----+ :
|
||||
: | Cons | 1 | o--+----+-----+--->| Cons | 2 | o--+--->| Nil | // | // | :
|
||||
: +------+----+----+ : : +------+----+----+ +------+----+----+ :
|
||||
: : : :
|
||||
: : : :
|
||||
'- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - - -'
|
||||
.- - - - - - - - - - - - - - . .- - - - - - - - - - - - - - - - - - - - - - - - -.
|
||||
: : : :
|
||||
: list : : :
|
||||
: +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
|
||||
: | Element | 1 | o--+----+-----+--->| Element | 2 | o--+--->| Nil | // | // | :
|
||||
: +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
|
||||
: : : :
|
||||
: : : :
|
||||
'- - - - - - - - - - - - - - ' '- - - - - - - - - - - - - - - - - - - - - - - - -'
|
||||
```
|
||||
<details>
|
||||
|
||||
@ -79,12 +81,12 @@ element of the `List` in the heap.
|
||||
```rust,editable
|
||||
#[derive(Debug)]
|
||||
enum List<T> {
|
||||
Cons(T, Box<List<T>>),
|
||||
Element(T, Box<List<T>>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
|
||||
let list: List<i32> = List::Element(1, Box::new(List::Element(2, Box::new(List::Nil))));
|
||||
println!("{list:?}");
|
||||
}
|
||||
```
|
||||
@ -94,15 +96,15 @@ allows the compiler to optimize the memory layout:
|
||||
|
||||
```bob
|
||||
Stack Heap
|
||||
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - -.
|
||||
: : : :
|
||||
: list : : :
|
||||
: +----+----+ : : +----+----+ +----+------+ :
|
||||
: | 1 | o--+-----------+-----+--->| 2 | o--+--->| // | null | :
|
||||
: +----+----+ : : +----+----+ +----+------+ :
|
||||
: : : :
|
||||
: : : :
|
||||
`- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - -'
|
||||
.- - - - - - - - - - - - - - . .- - - - - - - - - - - - - -.
|
||||
: : : :
|
||||
: list : : :
|
||||
: +---------+----+----+ : : +---------+----+----+ :
|
||||
: | Element | 1 | o--+----+-----+--->| Element | 2 | // | :
|
||||
: +---------+----+----+ : : +---------+----+----+ :
|
||||
: : : :
|
||||
: : : :
|
||||
'- - - - - - - - - - - - - - ' '- - - - - - - - - - - - - -'
|
||||
```
|
||||
|
||||
</details>
|
||||
|
Loading…
x
Reference in New Issue
Block a user