Created
January 11, 2014 05:23
-
-
Save pzol/8367436 to your computer and use it in GitHub Desktop.
list test in rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(dead_code, dead_assignment, unused_variable)]; | |
// recursive types | |
enum List<T> { | |
Cons(T, ~List<T>), | |
Empty | |
} | |
fn length<T>(xs: &List<T>) -> uint { | |
match *xs { | |
Empty => 0, | |
Cons(_, ref rest) => 1 + length(*rest) | |
} | |
} | |
fn prepend<'a, T>(xs: ~List<T>, value: T) -> List<T> { | |
Cons(value, xs) | |
} | |
// return a reference to the head value | |
fn head<'a, T>(xs: &'a List<T>) -> Option<&'a T> { | |
match *xs { | |
Cons(ref value, _) => Some(value), | |
_ => None | |
} | |
} | |
#[test] | |
fn test_length() { | |
let mut list: List<int> = Empty::<int>; | |
list = prepend(~list, 1); | |
list = prepend(~list, 2); | |
let list_len = length(&list); | |
assert_eq!(list_len, 2); | |
let h = head(&list); | |
println!("{:?}", h); | |
assert_eq!(h, Some(&2)); | |
} | |
enum IntList { | |
Null, | |
IntCons(int, ~IntList) | |
} | |
#[test] | |
fn test_int_list() { | |
let a = 1; | |
let b = a; | |
assert_eq!(a, 1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment