1
0
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:
Dustin J. Mitchell 2023-12-13 07:51:34 -05:00 committed by GitHub
parent 35442ad424
commit 9563f055e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 26 deletions

View File

@ -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 {

View File

@ -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);

View File

@ -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>