Skip to content

Instantly share code, notes, and snippets.

@bugaevc bugaevc/rust_ex4.rs
Last active May 2, 2016

Embed
What would you like to do?
// 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
You can’t perform that action at this time.