gh-pages
simonsan 3 years ago
parent 44f480cd0f
commit 241628a101

@ -185,20 +185,13 @@ enum MyEnum {
}
fn a_to_b(e: &mut MyEnum) {
// we mutably borrow `e` here. This precludes us from changing it directly
// as in `*e = ...`, because the borrow checker won't allow it. Therefore
// the assignment to `e` must be outside the `if let` clause.
*e = if let MyEnum::A { ref mut name, x: 0 } = *e {
if let MyEnum::A { name, x: 0 } = e {
// this takes out our `name` and put in an empty String instead
// (note that empty strings don't allocate).
// Then, construct the new enum variant (which will
// be assigned to `*e`, because it is the result of the `if let` expression).
MyEnum::B { name: mem::take(name) }
// In all other cases, we return immediately, thus skipping the assignment
} else { return }
// be assigned to `*e`).
*e = MyEnum::B { name: mem::take(name) }
}
}
<span class="boring">}
</span></code></pre></pre>
@ -217,11 +210,11 @@ enum MultiVariateEnum {
fn swizzle(e: &amp;mut MultiVariateEnum) {
use MultiVariateEnum::*;
*e = match *e {
*e = match e {
// Ownership rules do not allow taking `name` by value, but we cannot
// take the value out of a mutable reference, unless we replace it:
A { ref mut name } =&gt; B { name: mem::take(name) },
B { ref mut name } =&gt; A { name: mem::take(name) },
A { name } =&gt; B { name: mem::take(name) },
B { name } =&gt; A { name: mem::take(name) },
C =&gt; D,
D =&gt; C
}

@ -578,20 +578,13 @@ enum MyEnum {
}
fn a_to_b(e: &amp;mut MyEnum) {
// we mutably borrow `e` here. This precludes us from changing it directly
// as in `*e = ...`, because the borrow checker won't allow it. Therefore
// the assignment to `e` must be outside the `if let` clause.
*e = if let MyEnum::A { ref mut name, x: 0 } = *e {
if let MyEnum::A { name, x: 0 } = e {
// this takes out our `name` and put in an empty String instead
// (note that empty strings don't allocate).
// Then, construct the new enum variant (which will
// be assigned to `*e`, because it is the result of the `if let` expression).
MyEnum::B { name: mem::take(name) }
// In all other cases, we return immediately, thus skipping the assignment
} else { return }
// be assigned to `*e`).
*e = MyEnum::B { name: mem::take(name) }
}
}
<span class="boring">}
</span></code></pre></pre>
@ -610,11 +603,11 @@ enum MultiVariateEnum {
fn swizzle(e: &amp;mut MultiVariateEnum) {
use MultiVariateEnum::*;
*e = match *e {
*e = match e {
// Ownership rules do not allow taking `name` by value, but we cannot
// take the value out of a mutable reference, unless we replace it:
A { ref mut name } =&gt; B { name: mem::take(name) },
B { ref mut name } =&gt; A { name: mem::take(name) },
A { name } =&gt; B { name: mem::take(name) },
B { name } =&gt; A { name: mem::take(name) },
C =&gt; D,
D =&gt; C
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save