Skip to content

Instantly share code, notes, and snippets.

@yshui
Created January 20, 2015 02:58
Show Gist options
  • Save yshui/be366410dc60638160cc to your computer and use it in GitHub Desktop.
Save yshui/be366410dc60638160cc to your computer and use it in GitHub Desktop.
#![feature(box_syntax)]
use std::ops::DerefMut;
use std::ops::Deref;
#[derive(Show)]
struct List<T> {
car: T,
cdr: Option<Box<List<T>>>,
}
fn remove<T: Eq+Clone>(node: Box<List<T>>, pv: T, value: T) -> List<T> {
let mut now = node;
let tmp = now.car.clone();
if tmp == value {
return List { car: pv, cdr: now.cdr };
} else {
match now.cdr {
None => (),
Some(n) => now = box remove(n, tmp, value),
};
return List { car: pv, cdr: 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)})
}
let tmp = head.car.clone();
head = box remove(head, tmp, 5);
println!("{:?}", head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment