This commit is contained in:
2020-03-22 18:59:56 +08:00
parent 72f6e24bb9
commit 75682bb6c7

39
single_file_tests/list.rs Normal file
View File

@@ -0,0 +1,39 @@
enum List {
Cons(u32, Box<List>),
Nil,
}
impl List {
fn new() -> List {
List::Nil
}
fn prepend(self, element: u32) -> List {
List::Cons(element, Box::new(self))
}
fn len(&self) -> u32 {
match *self {
List::Nil => 0_u32,
List::Cons(_, ref next) => 1 + next.len(),
}
}
fn stringify(&self) -> String {
match *self {
List::Nil => format!("Nil"),
List::Cons(this, ref next) => format!("{} -> {}", this, next.stringify()),
}
}
}
fn main() {
let mut list = List::new();
list = list.prepend(1);
list = list.prepend(2);
list = list.prepend(3);
println!("Len: {}", list.len());
println!("Str: {}", list.stringify());
}