RBE custom types enum testcase linked list
This commit is contained in:
parent
cbcf9ce5fc
commit
f40981544c
@ -0,0 +1,45 @@
|
||||
use crate::List::*;
|
||||
|
||||
enum List {
|
||||
Cons(u32, Box<List>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
impl List {
|
||||
fn new() -> List {
|
||||
Nil
|
||||
}
|
||||
|
||||
fn prepend(self, elem: u32) -> List {
|
||||
Cons(elem, Box::new(self))
|
||||
}
|
||||
|
||||
fn len(&self) -> u32 {
|
||||
match *self {
|
||||
Cons(_, ref tail) => 1 + tail.len(),
|
||||
Nil => 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn stringify(&self) -> String {
|
||||
match *self {
|
||||
Cons(head, ref tail) => {
|
||||
format!("{}, {}", head, tail.stringify())
|
||||
}
|
||||
Nil => {
|
||||
format!("Nil")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut list = List::new();
|
||||
|
||||
list = list.prepend(1);
|
||||
list = list.prepend(2);
|
||||
list = list.prepend(3);
|
||||
|
||||
println!("linked list has length: {}", list.len());
|
||||
println!("{}", list.stringify());
|
||||
}
|
Loading…
Reference in New Issue
Block a user