Skip to content

Commit d827cec

Browse files
committed
Add another example to the mem::replace idiom
This fixes #38
1 parent 67870c8 commit d827cec

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

idioms/mem-replace.md

+22
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ fn a_to_b(e: &mut MyEnum) {
3636
}
3737
```
3838

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+
A { ref mut name } => B { name: mem::replace(name, String::new()) },
55+
B { ref mut name } => A { name: mem::replace(name, String::new()) },
56+
C => D,
57+
D => C
58+
}
59+
}
60+
```
3961

4062
## Motivation
4163

0 commit comments

Comments
 (0)