1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-16 22:27:34 +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
3 changed files with 30 additions and 26 deletions

View File

@ -42,6 +42,8 @@ struct Package {
impl Package { impl Package {
// ANCHOR_END: Package // ANCHOR_END: Package
// ANCHOR: as_dependency // ANCHOR: as_dependency
/// Return a representation of this package as a dependency, for use in
/// building other packages.
fn as_dependency(&self) -> Dependency { fn as_dependency(&self) -> Dependency {
// ANCHOR_END: as_dependency // ANCHOR_END: as_dependency
Dependency { Dependency {

View File

@ -2,10 +2,10 @@
minutes: 10 minutes: 10
--- ---
# Lifetimes # Lifetime Annotations
A reference has a _lifetime_, which must "outlive" the value it refers to. This A reference has a _lifetime_, which must not "outlive" the value it refers to.
is verified by the borrow checker. This is verified by the borrow checker.
The lifetime can be implicit - this is what we have seen so far. Lifetimes can 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 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 Lifetimes become more complicated when considering passing values to and
returning values from functions. returning values from functions.
```rust,compile_fail ```rust,eitable,compile_fail
#[derive(Debug)] #[derive(Debug)]
struct Point(i32, i32); 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 ```rust,editable
#[derive(Debug)] #[derive(Debug)]
enum List<T> { 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, Nil,
} }
fn main() { 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:?}"); println!("{list:?}");
} }
``` ```
```bob ```bob
Stack Heap Stack Heap
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - - -. .- - - - - - - - - - - - - - . .- - - - - - - - - - - - - - - - - - - - - - - - -.
: : : : : : : :
: list : : : : list : : :
: +------+----+----+ : : +------+----+----+ +------+----+----+ : : +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
: | Cons | 1 | o--+----+-----+--->| Cons | 2 | o--+--->| Nil | // | // | : : | Element | 1 | o--+----+-----+--->| Element | 2 | o--+--->| Nil | // | // | :
: +------+----+----+ : : +------+----+----+ +------+----+----+ : : +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
: : : : : : : :
: : : : : : : :
'- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - - -' '- - - - - - - - - - - - - - ' '- - - - - - - - - - - - - - - - - - - - - - - - -'
``` ```
<details> <details>
@ -79,12 +81,12 @@ element of the `List` in the heap.
```rust,editable ```rust,editable
#[derive(Debug)] #[derive(Debug)]
enum List<T> { enum List<T> {
Cons(T, Box<List<T>>), Element(T, Box<List<T>>),
Nil, Nil,
} }
fn main() { 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:?}"); println!("{list:?}");
} }
``` ```
@ -94,15 +96,15 @@ allows the compiler to optimize the memory layout:
```bob ```bob
Stack Heap Stack Heap
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - -. .- - - - - - - - - - - - - - . .- - - - - - - - - - - - - -.
: : : : : : : :
: list : : : : list : : :
: +----+----+ : : +----+----+ +----+------+ : : +---------+----+----+ : : +---------+----+----+ :
: | 1 | o--+-----------+-----+--->| 2 | o--+--->| // | null | : : | Element | 1 | o--+----+-----+--->| Element | 2 | // | :
: +----+----+ : : +----+----+ +----+------+ : : +---------+----+----+ : : +---------+----+----+ :
: : : : : : : :
: : : : : : : :
`- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - -' '- - - - - - - - - - - - - - ' '- - - - - - - - - - - - - -'
``` ```
</details> </details>