Created
January 24, 2014 16:07
-
-
Save chrisvest/8600340 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
// Using Rust 0.9: | |
#[feature(struct_variant)]; | |
enum X { | |
Y { y: ~str } | |
} | |
impl X { | |
fn foo(&self) { | |
match self { | |
&Y { y } => println!("y = {}", y) | |
} | |
} | |
} | |
fn main() { | |
let s = ~"abc"; | |
let x = Y { y: s }; | |
x.foo(); | |
x.foo(); | |
} | |
// $ rustc test.rs && ./test | |
// test.rs:11:5: 11:12 error: cannot move out of dereference of & pointer | |
// test.rs:11 &Y { y } => println!("y = {}", y) | |
// ^~~~~~~ | |
// error: aborting due to previous error | |
// task 'rustc' failed at 'explicit failure', /private/tmp/rust-R5p2/rust-0.9/src/libsyntax/diagnostic.rs:75 | |
// task '<main>' failed at 'explicit failure', /private/tmp/rust-R5p2/rust-0.9/src/librustc/lib.rs:453 | |
// $ |
Yeah, that was the problem. I think the tutorial was a bit unclear about that part, as I tried both ~ref
and &ref
before finding a similar question on stackoverflow.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try
&Y { ref y }
instead of&Y { y }
in your pattern, to indicate that you want to bindy
as a reference into whateverself
is pointing to, rather than attempting to move those contents out of whateverself
points to (which you are not allowed to do).