mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-27 22:34:14 +00:00
Improve message for rustc --explain E0507
E0507 can occur when you try to move out of a member of a mutably borrowed struct, in which case `mem::replace` can help. Mentioning that here hopefully saves future users a trip to Google.
This commit is contained in:
parent
b8b18aac12
commit
f841f061ec
@ -377,6 +377,33 @@ fn main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Moving out of a member of a mutably borrowed struct is fine if you put something
|
||||||
|
back. `mem::replace` can be used for that:
|
||||||
|
|
||||||
|
```
|
||||||
|
struct TheDarkKnight;
|
||||||
|
|
||||||
|
impl TheDarkKnight {
|
||||||
|
fn nothing_is_true(self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Batcave {
|
||||||
|
knight: TheDarkKnight
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
|
let mut cave = Batcave {
|
||||||
|
knight: TheDarkKnight
|
||||||
|
};
|
||||||
|
let borrowed = &mut cave;
|
||||||
|
|
||||||
|
borrowed.knight.nothing_is_true(); // E0507
|
||||||
|
mem::replace(&mut borrowed.knight, TheDarkKnight).nothing_is_true(); // ok!
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
You can find more information about borrowing in the rust-book:
|
You can find more information about borrowing in the rust-book:
|
||||||
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
|
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
|
||||||
"##,
|
"##,
|
||||||
|
Loading…
Reference in New Issue
Block a user