Skip to content

Instantly share code, notes, and snippets.

@eightbitraptor
Last active August 29, 2015 14:02
Show Gist options
  • Save eightbitraptor/42a5055805326018b61f to your computer and use it in GitHub Desktop.
Save eightbitraptor/42a5055805326018b61f to your computer and use it in GitHub Desktop.
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