Skip to content

Instantly share code, notes, and snippets.

@chrisvest
Created January 24, 2014 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvest/8600340 to your computer and use it in GitHub Desktop.
Save chrisvest/8600340 to your computer and use it in GitHub Desktop.
// 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
// $
@pnkfelix
Copy link

try &Y { ref y } instead of &Y { y } in your pattern, to indicate that you want to bind y as a reference into whatever self is pointing to, rather than attempting to move those contents out of whatever self points to (which you are not allowed to do).

@chrisvest
Copy link
Author

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