Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
In [ ]:
:timing
:sccache 1
In [2]:
let b = Box::new(5);
b
Out[2]:
In [3]:
println!("b = {}", b);
In [4]:
*b
Out[4]:
Use Box to Construct Recursive Types¶
In [6]:
enum List {
Cons(i32, List),
Nil,
}
In [3]:
#[derive(Debug)]
enum List {
Cons(i32, Box<List>),
Nil,
}
use List::{Cons, Nil};
In [4]:
let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil))))));
list
Out[4]:
In [10]:
match list {
Cons(val, tail) => {
println!("{}", val);
},
_ => println!("End of the list"),
}
Out[10]:
In [ ]: