We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 67870c8 + 964dd99 commit 3b37252Copy full SHA for 3b37252
idioms/mem-replace.md
@@ -36,6 +36,30 @@ fn a_to_b(e: &mut MyEnum) {
36
}
37
```
38
39
+This also works with more variants:
40
+
41
+```Rust
42
+use std::mem;
43
44
+enum MultiVariateEnum {
45
+ A { name: String },
46
+ B { name: String },
47
+ C,
48
+ D
49
+}
50
51
+fn swizzle(e: &mut MultiVariateEnum) {
52
+ use self::MultiVariateEnum::*;
53
+ *e = match *e {
54
+ // Ownership rules do not allow taking `name` by value, but we cannot
55
+ // take the value out of a mutable reference, unless we replace it:
56
+ A { ref mut name } => B { name: mem::replace(name, String::new()) },
57
+ B { ref mut name } => A { name: mem::replace(name, String::new()) },
58
+ C => D,
59
+ D => C
60
+ }
61
62
+```
63
64
## Motivation
65
0 commit comments