Box<T>
를 사용하여 힙에 데이터 저장fn main(){
let b = Box::new(5);
println!("b={}",b);
}
재귀적 타입
은 크기를 알 수가 없음 enum List{
Cons(i32, List),
Nil,
}
use List::{Cons, Nil};
fn main(){
let list = Cons(1, Cons(2, Cons(3, Nil)));
}
error[E0072]: recursive type `List` has infinite size
--> src/main.rs:1:1
|
1 | enum List {
| ^^^^^^^^^ recursive type has infinite size
2 | Cons(i32, List),
| ----- recursive without indirection
|
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to
make `List` representable
fn main(){
let list = Cons(1,
Box::new(Cons(2,
Box::new(Cons(3,
Box::new(Nil))))));
}