Skip to content

Instantly share code, notes, and snippets.

@donabrams
Forked from anonymous/playground.rs
Last active December 22, 2015 18:06
Show Gist options
  • Save donabrams/b6c326866affeba1f265 to your computer and use it in GitHub Desktop.
Save donabrams/b6c326866affeba1f265 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
}
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
}
@donabrams
Copy link
Author

: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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment