1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-07-12 09:10:40 +02:00

Publish Comprehensive Rust 🦀

This commit is contained in:
Martin Geisler
2022-12-21 16:36:30 +01:00
commit c212a473ba
252 changed files with 8047 additions and 0 deletions

32
src/std/box-recursive.md Normal file
View File

@ -0,0 +1,32 @@
# Box with Recursive Data Structures
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>>),
Nil,
}
fn main() {
let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
println!("{list:?}");
}
```
```bob
Stack Heap
.- - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - - -.
: : : :
: list : : :
: +--------+-------+ : : +--------+--------+ +--------+------+ :
: | Tag | Cons | : : .->| Tag | Cons | .->| Tag | Nil | :
: | 0 | 1 | : : | | 0 | 2 | | | ////// | //// | :
: | 1 | o-----+----+-----+-' | 1 | o------+-' | ////// | //// | :
: +--------+-------+ : : +--------+--------+ +--------+------+ :
: : : :
: : : :
`- - - - - - - - - - - - -' '- - - - - - - - - - - - - - - - - - - - - - - -'
```