Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use crate::List::{Cons, Nil};
- #[derive(Debug)]
- enum List {
- Cons(i32, Box<List>),
- Nil,
- }
- fn main() {
- let mut list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil))))));
- println!("The first value is: {}.", list.pop().unwrap());
- println!("The second value is: {}.", list.pop().unwrap());
- }
- impl List {
- fn pop(&mut self) -> Option<i32> {
- let old_list = (self, Nil);
- match old_list {
- Cons(value, tail) => {
- *self = *tail;
- Some(value)
- }
- Nil => None,
- }
- }
- }
Advertisement
Advertisement