Skip to content

Instantly share code, notes, and snippets.

@bugaevc
Last active May 2, 2016 06:32
Show Gist options
  • Save bugaevc/b16483e4c81d360eb03069761d4b48e2 to your computer and use it in GitHub Desktop.
Save bugaevc/b16483e4c81d360eb03069761d4b48e2 to your computer and use it in GitHub Desktop.
// takes v by (immutable) reference
fn count_occurences(v: &Vec<i32>, val: i32) -> usize {
v.into_iter().filter(|&&x| x == val).count()
}
fn main() {
let v = vec![2, 9, 3, 1, 3, 2, 5, 5, 2];
// borrowing v for the iteration
for &item in &v {
// the first borrow is still active
// we borrow it the second time here!
let res = count_occurences(&v, item);
println!("{} is repeated {} times", item, res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment