more idiomatic with closure
This commit is contained in:
parent
beb5272b61
commit
0cf2b5c247
1 changed files with 8 additions and 11 deletions
19
src/main.rs
19
src/main.rs
|
|
@ -4,8 +4,8 @@ fn main() {
|
||||||
println!("push(1) : {:?}", list);
|
println!("push(1) : {:?}", list);
|
||||||
list.push(2);
|
list.push(2);
|
||||||
println!("push(2) : {:?}", list);
|
println!("push(2) : {:?}", list);
|
||||||
println!("pop() -> {} : {:?}",list.pop().unwrap(),list);
|
println!("pop() -> {} : {:?}", list.pop().unwrap(), list);
|
||||||
println!("pop() -> {} : {:?}",list.pop().unwrap(),list);
|
println!("pop() -> {} : {:?}", list.pop().unwrap(), list);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -21,7 +21,7 @@ struct Node<T> {
|
||||||
next: Link<T>,
|
next: Link<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> List<T> {
|
impl<T> List<T> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { head: None }
|
Self { head: None }
|
||||||
}
|
}
|
||||||
|
|
@ -35,13 +35,10 @@ impl <T> List<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop(&mut self) -> Option<T> {
|
pub fn pop(&mut self) -> Option<T> {
|
||||||
match self.head.take() {
|
self.head.take().map(|node| {
|
||||||
None => None,
|
self.head = node.next;
|
||||||
Some(node) => {
|
node.elem
|
||||||
self.head = node.next;
|
})
|
||||||
Some(node.elem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,6 +51,6 @@ mod tests {
|
||||||
fn test() {
|
fn test() {
|
||||||
let mut list = List::new();
|
let mut list = List::new();
|
||||||
list.push(42);
|
list.push(42);
|
||||||
println!("{:?}", list);
|
assert_eq!(Some(42), list.pop());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue