-
-
Save donabrams/b6c326866affeba1f265 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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 main() { | |
let mut p = get_mut(); | |
for &x in p.iter() { | |
print!("{} ", x); | |
} | |
println!(""); | |
} | |
fn get_mut<'s>() -> &'s mut Vec<i16> { | |
let mut x: Vec<i16> = vec![1, 2, 3]; | |
for i in x.iter_mut() { | |
*i += 1; | |
} | |
&mut x | |
} |
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 main() { | |
let mut p = get_mut(); | |
while let Some(x) = p.pop() { | |
print!("{} ", x); | |
} | |
println!(""); | |
} | |
fn get_mut<'s>() -> Vec<i16> { | |
let mut x: Vec<i16> = vec![1, 2, 3]; | |
for i in x.iter_mut() { | |
*i += 1; | |
} | |
x | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:15:10: 15:11 error:
x
does not live long enough:15 &mut x
^
:10:38: 16:2 note: reference must be valid for the lifetime 's as defined on the block at 10:37...
:10 fn get_mut<'s>() -> &'s mut Vec {
:11 let mut x: Vec = vec![1, 2, 3];
:12 for i in x.iter_mut() {
:13 *i += 1;
:14 }
:15 &mut x
...
:11:41: 16:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 11:40
:11 let mut x: Vec = vec![1, 2, 3];
:12 for i in x.iter_mut() {
:13 *i += 1;
:14 }
:15 &mut x
:16 }
error: aborting due to previous error
playpen: application terminated with error code 101