Skip to content

Instantly share code, notes, and snippets.

@tioover
Last active August 29, 2015 14:13
Show Gist options
  • Save tioover/8d7585105c06e01678a8 to your computer and use it in GitHub Desktop.
Save tioover/8d7585105c06e01678a8 to your computer and use it in GitHub Desktop.
use std::ops::DerefMut;
use std::ops::Deref;
struct List<T> {
car: Option<T>,
cdr: Option<Box<List<T>>>,
}
fn main() {
let mut a:List<i8> = List {car: None, cdr: None};
{
let mut tail = &mut a;
for i in range(1, 42i8) {
tail.cdr = Some(Box::new(List {car: Some(i), cdr: None}));
tail = tail.cdr.unwrap().deref_mut();
}
}
}
// test.rs:16:20: 16:37 error: borrowed value does not live long enough
// test.rs:16 tail = tail.cdr.unwrap().deref_mut();
// ^~~~~~~~~~~~~~~~~
// test.rs:12:5: 18:6 note: reference must be valid for the block at 12:4...
// test.rs:12 {
// test.rs:13 let mut tail = &mut a;
// test.rs:14 for i in range(1, 42i8) {
// test.rs:15 tail.cdr = Some(Box::new(List {car: Some(i), cdr: None}));
// test.rs:16 tail = tail.cdr.unwrap().deref_mut();
// test.rs:17 }
// ...
// test.rs:16:13: 16:50 note: ...but borrowed value is only valid for the statement at 16:12
// test.rs:16 tail = tail.cdr.unwrap().deref_mut();
// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test.rs:16:13: 16:50 help: consider using a `let` binding to increase its lifetime
// test.rs:16 tail = tail.cdr.unwrap().deref_mut();
// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// test.rs:16:20: 16:24 error: cannot move out of borrowed content
// test.rs:16 tail = tail.cdr.unwrap().deref_mut();
// ^~~~
// error: aborting due to 2 previous errors
use std::ops::DerefMut;
use std::ops::Deref;
struct List<T> {
car: Option<T>,
cdr: Option<Box<List<T>>>,
}
fn main() {
let mut a:List<i8> = List {car: None, cdr: None};
{
let mut tail = &mut a;
let mut temp;
for i in range(1, 42i8) {
tail.cdr = Some(Box::new(List {car: Some(i), cdr: None}));
temp = tail.cdr.unwrap();
tail = temp.deref_mut();
}
}
}
// test.rs:17:20: 17:24 error: cannot move out of borrowed content
// test.rs:17 temp = tail.cdr.unwrap();
// ^~~~
// test.rs:17:13: 17:37 error: cannot assign to `temp` because it is borrowed
// test.rs:17 temp = tail.cdr.unwrap();
// ^~~~~~~~~~~~~~~~~~~~~~~~
// test.rs:18:20: 18:24 note: borrow of `temp` occurs here
// test.rs:18 tail = temp.deref_mut();
// ^~~~
// test.rs:18:20: 18:24 error: cannot borrow `temp` as mutable more than once at a time
// test.rs:18 tail = temp.deref_mut();
// ^~~~
// test.rs:18:20: 18:24 note: previous borrow of `temp` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `temp` until the borrow ends
// test.rs:18 tail = temp.deref_mut();
// ^~~~
// test.rs:20:6: 20:6 note: previous borrow ends here
// test.rs:12 {
// ...
// test.rs:20 }
// ^
// error: aborting due to 3 previous errors
use std::ops::DerefMut;
use std::ops::Deref;
struct List<T> {
car: Option<T>,
cdr: Option<Box<List<T>>>,
}
fn main() {
let mut a:List<i8> = List {car: None, cdr: None};
{
let mut tail = &mut a;
for i in range(1, 42i8) {
let new = Box::new(List {car: Some(i), cdr: None});
let next = new.deref_mut();
tail.cdr = Some(new);
tail = next;
}
}
}
// test.rs:16:24: 16:27 error: `new` does not live long enough
// test.rs:16 let next = new.deref_mut();
// ^~~
// test.rs:12:5: 20:6 note: reference must be valid for the block at 12:4...
// test.rs:12 {
// test.rs:13 let mut tail = &mut a;
// test.rs:14 for i in range(1, 42i8) {
// test.rs:15 let new = Box::new(List {car: Some(i), cdr: None});
// test.rs:16 let next = new.deref_mut();
// test.rs:17 tail.cdr = Some(new);
// ...
// test.rs:14:33: 19:10 note: ...but borrowed value is only valid for the block at 14:32
// test.rs:14 for i in range(1, 42i8) {
// test.rs:15 let new = Box::new(List {car: Some(i), cdr: None});
// test.rs:16 let next = new.deref_mut();
// test.rs:17 tail.cdr = Some(new);
// test.rs:18 tail = next;
// test.rs:19 }
// error: aborting due to previous error
use std::ops::DerefMut;
use std::ops::Deref;
struct List<T> {
car: Option<T>,
cdr: Option<Box<List<T>>>,
}
fn main() {
let mut a:List<i8> = List {car: None, cdr: None};
{
let mut tail = &mut a;
let mut temp: &mut List<i8>;
for i in range(1, 42i8) {
let new = Box::new(List {car: Some(i), cdr: None});
let temp = new.deref_mut();
tail.cdr = Some(new);
tail = temp;
}
}
}
// test.rs:17:24: 17:27 error: `new` does not live long enough
// test.rs:17 let temp = new.deref_mut();
// ^~~
// test.rs:12:5: 21:6 note: reference must be valid for the block at 12:4...
// test.rs:12 {
// test.rs:13 let mut tail = &mut a;
// test.rs:14 let mut temp: &mut List<i8>;
// test.rs:15 for i in range(1, 42i8) {
// test.rs:16 let new = Box::new(List {car: Some(i), cdr: None});
// test.rs:17 let temp = new.deref_mut();
// ...
// test.rs:15:33: 20:10 note: ...but borrowed value is only valid for the block at 15:32
// test.rs:15 for i in range(1, 42i8) {
// test.rs:16 let new = Box::new(List {car: Some(i), cdr: None});
// test.rs:17 let temp = new.deref_mut();
// test.rs:18 tail.cdr = Some(new);
// test.rs:19 tail = temp;
// test.rs:20 }
// error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment