You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-16 14:17: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:
committed by
GitHub
parent
35442ad424
commit
9563f055e2
@ -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>
|
||||
|
Reference in New Issue
Block a user