Last active
August 29, 2015 14:13
-
-
Save tioover/f9632449aab6fce840a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ops::DerefMut; | |
use std::ops::Deref; | |
#[derive(Show)] | |
struct List<T> { | |
car: T, | |
cdr: Option<Box<List<T>>>, | |
} | |
fn remove<T: Eq>(node: Option<Box<List<T>>>, value: T) -> Option<Box<List<T>>> { | |
match node { | |
None => None, | |
Some(node) => { | |
let mut now = node; | |
if now.car == value { now.cdr } | |
else { | |
now.cdr = remove(now.cdr.take(), value); | |
Some(now) | |
} | |
} | |
} | |
} | |
fn main() { | |
let mut head = Box::new(List {car: 0i8, cdr: None}); | |
for i in range(1, 42i8) { | |
head = Box::new(List {car: i, cdr: Some(head)}) | |
} | |
head = remove(Some(head), 5).unwrap(); | |
println!("{:?}", head); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment