Last active
August 29, 2015 14:02
-
-
Save eightbitraptor/42a5055805326018b61f to your computer and use it in GitHub Desktop.
This file contains 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
fn last_but_one(list: &Vec<int>) -> int { | |
// create a mutable copy of list | |
let mut mylist = list; | |
// reverse mylist in place | |
mylist.reverse(); | |
// get a reference to the second element and return it | |
let second: &int = mylist.get(1); | |
// dereference the pointer so I can return an actual int | |
*second | |
} | |
#[test] | |
fn test_gets_penultimate_element() { | |
let input = vec!(1, 5, 6, 7, 2, 4); | |
assert_eq!(last_but_one(&input), 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment