Created
November 25, 2015 11:48
-
-
Save jorendorff/87a702a1f6d2bef26d80 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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