Last active
August 29, 2015 14:13
-
-
Save tioover/526715ed05342ef5b4f1 to your computer and use it in GitHub Desktop.
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
use std::ops::DerefMut; | |
use std::ops::Deref; | |
struct List<T> { | |
car: T, | |
cdr: Option<Box<List<T>>>, | |
} | |
fn main() { | |
let mut head = Box::new(List {car: 0i8, cdr: None}); | |
for i in range(1, 42i8) { | |
head = Box::new(List {car: i, cdr: Some(head)}) | |
} | |
{ | |
let mut now = head.deref(); | |
loop { | |
match now.cdr { | |
None => break, | |
Some(ref x) => { | |
println!("{}", x.car); | |
now = x.deref(); | |
} | |
} | |
} | |
} | |
} | |
// succues compile and run. |
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
use std::ops::DerefMut; | |
use std::ops::Deref; | |
struct List<T> { | |
car: T, | |
cdr: Option<Box<List<T>>>, | |
} | |
fn main() { | |
let mut head = Box::new(List {car: 0i8, cdr: None}); | |
for i in range(1, 42i8) { | |
head = Box::new(List {car: i, cdr: Some(head)}) | |
} | |
{ | |
let mut now = head.deref_mut(); | |
loop { | |
match now.cdr { | |
None => break, | |
Some(ref mut x) => { | |
println!("{}", x.car); | |
now = x.deref_mut(); | |
} | |
} | |
} | |
} | |
} | |
// test.rs:21:22: 21:31 error: cannot borrow `now.cdr.0` as mutable more than once at a time | |
// test.rs:21 Some(ref mut x) => { | |
// ^~~~~~~~~ | |
// test.rs:21:22: 21:31 note: previous borrow of `now.cdr.0` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `now.cdr.0` until the borrow ends | |
// test.rs:21 Some(ref mut x) => { | |
// ^~~~~~~~~ | |
// test.rs:27:6: 27:6 note: previous borrow ends here | |
// test.rs:16 { | |
// ... | |
// test.rs:27 } | |
// ^ | |
// test.rs:23:21: 23:40 error: cannot assign to `now` because it is borrowed | |
// test.rs:23 now = x.deref_mut(); | |
// ^~~~~~~~~~~~~~~~~~~ | |
// test.rs:21:22: 21:31 note: borrow of `now` occurs here | |
// test.rs:21 Some(ref mut x) => { | |
// ^~~~~~~~~ | |
// error: aborting due to 2 previous errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment