// list.rs - list implementing ToStr trait struct Node { head : int, tail : Option<@Node> } type List = Option<@Node> ; impl ToStr for List { fn to_str(&self) -> ~str { fn elements_to_str(n: @Node) -> ~str { match (n.tail) { None => fmt!("%?", n.head), Some(tail) => fmt!("%?, %s", n.head, elements_to_str(tail)) } } match(*self) { None => ~"Null", Some(n) => fmt!("[%s]", elements_to_str(n)) } } } fn main() { let lst : List = Some(@Node{head: 1, tail: Some(@Node{head : 2, tail: Some(@Node{head: 3, tail: None})})}); println(fmt!("%s", lst.to_str())); }