Last active
April 17, 2020 08:22
-
-
Save tbarusseau/cd655da352fcd519d697dc156c02c78f 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
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