示例代码:
#[derive(Debug)]
enum List {
Cons(i32, Box<List>),
Nil,
}
// C语言的定义方式
// struct List {
// int value;
// struct List *next;
// // struct List l;
// };
//
fn main() {
use List::Cons;
use List::Nil;
//let list = Cons(1, Cons(2, Cons(3, Nil)));
let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil))))));
println!("{:?}", list);
}
本节全部源代码:
https://github.com/anonymousGiga/learn_rust/blob/master/learn_box1/src/main.rs
来源:oschina
链接:https://my.oschina.net/u/943779/blog/4922340