From 0cf2b5c2476440636caab8f1bb3293bdfd89719d Mon Sep 17 00:00:00 2001 From: Sander Hautvast Date: Mon, 14 Feb 2022 13:05:47 +0100 Subject: [PATCH] more idiomatic with closure --- src/main.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 65b3f39..46de473 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,8 @@ fn main() { println!("push(1) : {:?}", list); list.push(2); 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)] @@ -21,7 +21,7 @@ struct Node { next: Link, } -impl List { +impl List { pub fn new() -> Self { Self { head: None } } @@ -35,13 +35,10 @@ impl List { } pub fn pop(&mut self) -> Option { - match self.head.take() { - None => None, - Some(node) => { - self.head = node.next; - Some(node.elem) - } - } + self.head.take().map(|node| { + self.head = node.next; + node.elem + }) } } @@ -54,6 +51,6 @@ mod tests { fn test() { let mut list = List::new(); list.push(42); - println!("{:?}", list); + assert_eq!(Some(42), list.pop()); } }