Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Created November 25, 2015 11:48
Show Gist options
  • Select an option

  • Save jorendorff/87a702a1f6d2bef26d80 to your computer and use it in GitHub Desktop.

Select an option

Save jorendorff/87a702a1f6d2bef26d80 to your computer and use it in GitHub Desktop.
// transfering an object from a mutable binding to an immutable one
fn all_pairs(a: &[i32], b: &[i32]) -> Vec<(i32, i32)> {
// Build a vector. Note `result` is mutable throughout.
let mut result = Vec::new();
for &i in a {
for &j in b {
result.push((i, j));
}
}
result
}
fn main() {
// Now we'll call that function, and assign result to a non-mut variable.
// This makes the Vec immutable! The data is *not* copied;
// it's the same buffer. But the mutable methods are inaccessible.
let pairs = all_pairs(&[1, 2, 3], &[1, 4, 9]);
pairs.push((3, 6)); // error: immutable local variable `pairs`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment