Skip to content

Instantly share code, notes, and snippets.

@tbarusseau
Last active April 17, 2020 08:22
Show Gist options
  • Save tbarusseau/cd655da352fcd519d697dc156c02c78f to your computer and use it in GitHub Desktop.
Save tbarusseau/cd655da352fcd519d697dc156c02c78f to your computer and use it in GitHub Desktop.
fn sum(vector: &Vec<i32>) -> i32 {
let mut sum = 0;
// vector.iter_mut().map(|e| e.add(1));
// ERROR: cannot borrow `*vector` as mutable, as it is behind a `&` reference
for item in vector {
sum = sum + item
}
sum
}
fn main() {
let v = vec![1,2,3];
let s = sum(&v);
println!("sum of {:?}: {}", v, s); // Now valid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment