Skip to content

Instantly share code, notes, and snippets.

@tioover
Last active August 29, 2015 14:13
Show Gist options
  • Save tioover/f9632449aab6fce840a4 to your computer and use it in GitHub Desktop.
Save tioover/f9632449aab6fce840a4 to your computer and use it in GitHub Desktop.
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