diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2016-02-02 00:32:18 -0500 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2016-02-02 00:32:18 -0500 |
| commit | 674f71edd03b3df404d36fb188e7dda770a5cd1a (patch) | |
| tree | 2ec53e8f2ba35f2e8da7f755fe99502f62c24706 | |
| parent | e65f29a3b8aea46b9720767690b3671123813f16 (diff) | |
| parent | f841f061ecdff7c5fd791f164b4d873399621bde (diff) | |
Rollup merge of #31270 - ruud-v-a:improve-e0507, r=steveklabnik
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.
| -rw-r--r-- | src/librustc_borrowck/diagnostics.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/librustc_borrowck/diagnostics.rs b/src/librustc_borrowck/diagnostics.rs index 7ad4d3ca708..6cbea1abbb5 100644 --- a/src/librustc_borrowck/diagnostics.rs +++ b/src/librustc_borrowck/diagnostics.rs @@ -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: http://doc.rust-lang.org/stable/book/references-and-borrowing.html "##, |
