Skip to content

Instantly share code, notes, and snippets.

@jaapz
Last active August 29, 2015 14:12
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 jaapz/dd46774f98029999128d to your computer and use it in GitHub Desktop.
Save jaapz/dd46774f98029999128d to your computer and use it in GitHub Desktop.
Why does this happen?
struct Record {
name: String,
message: String,
}
// Let's say `record` is an instance of Record, with strings in name and message.
// This will work correctly:
let message =
if some_condition {
record.name.as_slice()
} else {
record.message.as_slice()
};
// This will break, saying error: cannot move out of dereference of `&`-pointer.
let message =
if some_condition {
record.name
} else {
record.message
};
// This is how i create the records
let mut records: Vec<Record> = vec![];
for record in reader.decode() {
let record: Record = record.unwrap();
records.push(record);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment