#[deriving(Show)] pub enum List { Nil, Cons(A, Box>) } // xs: doesn't work as a reference, probably as Box? pub fn cons(x: A, xs: List) -> List { match xs { Nil => Cons(x, box Nil), l => Cons(x, box l) } } pub fn first(xs: &List) -> Option<&A> { match xs { &Nil => None, &Cons(ref x, _) => Some(x) } } // nth // map - does this make sense? (e.g. we probably don't want to copy everything.) should it be lazy? // fn map(..., &List<&A>) -> List // or -> List<&B>? // filter - we want references to the old values and keep the existing cons cells // reduce pub fn main() { let nil: &List = &Nil; let ns: &List = &cons(1i, cons(2, cons(3, Nil))); println!("nil = {}, ns = {}", nil, ns); println!("first(nil) = {}, first(ns) = {}", first(nil), first(ns)); }