Skip to content

Instantly share code, notes, and snippets.

@tautologico
Last active March 12, 2016 23:37
Show Gist options
  • Save tautologico/7146153 to your computer and use it in GitHub Desktop.
Save tautologico/7146153 to your computer and use it in GitHub Desktop.
Integer lists in Rust.
// Functional-style integer lists
// Compile with "rustc -o intlist intlist.rs"
// more recent versions of the compiler need this for @ pointers
#[feature(managed_boxes)];
enum IntList {
Empty,
Cons(int, @IntList)
}
fn length(l: @IntList) -> uint {
match *l {
Empty => 0,
Cons(_, rest) => 1 + length(rest)
}
}
fn sum_list(l: @IntList) -> int {
match *l {
Empty => 0,
Cons(i, rest) => i + sum_list(rest)
}
}
fn main()
{
let l1 = @Cons(5, @Cons(3, @Cons(7, @Empty)));
println!("length of l1 is {}", length(l1));
println!("sum of l1 is {}", sum_list(l1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment