Skip to content

Commit c2ca3e9

Browse files
authored
Merge branch 'master' into state_pattern
2 parents cd0150c + 22de99a commit c2ca3e9

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

idioms/mem-replace.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,13 @@ enum MyEnum {
1919
}
2020

2121
fn a_to_b(e: &mut MyEnum) {
22-
23-
// we mutably borrow `e` here. This precludes us from changing it directly
24-
// as in `*e = ...`, because the borrow checker won't allow it. Therefore
25-
// the assignment to `e` must be outside the `if let` clause.
26-
*e = if let MyEnum::A { ref mut name, x: 0 } = *e {
27-
22+
if let MyEnum::A { name, x: 0 } = e {
2823
// this takes out our `name` and put in an empty String instead
2924
// (note that empty strings don't allocate).
3025
// Then, construct the new enum variant (which will
31-
// be assigned to `*e`, because it is the result of the `if let` expression).
32-
MyEnum::B { name: mem::take(name) }
33-
34-
// In all other cases, we return immediately, thus skipping the assignment
35-
} else { return }
26+
// be assigned to `*e`).
27+
*e = MyEnum::B { name: mem::take(name) }
28+
}
3629
}
3730
```
3831

@@ -50,11 +43,11 @@ enum MultiVariateEnum {
5043

5144
fn swizzle(e: &mut MultiVariateEnum) {
5245
use MultiVariateEnum::*;
53-
*e = match *e {
46+
*e = match e {
5447
// Ownership rules do not allow taking `name` by value, but we cannot
5548
// take the value out of a mutable reference, unless we replace it:
56-
A { ref mut name } => B { name: mem::take(name) },
57-
B { ref mut name } => A { name: mem::take(name) },
49+
A { name } => B { name: mem::take(name) },
50+
B { name } => A { name: mem::take(name) },
5851
C => D,
5952
D => C
6053
}

0 commit comments

Comments
 (0)