Last active
May 2, 2016 06:32
-
-
Save bugaevc/b16483e4c81d360eb03069761d4b48e2 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
// 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