Adressed comments

pull/31/head
Andre Bogus 8 years ago
parent 31b282ad9f
commit 547ff18605

@ -22,6 +22,7 @@ language.
* [Iterating over an `Option`](idioms/option-iter.md)
* TODO `Default` trait
* [Pass variables to closure](idioms/pass-var-to-closure.md)
* [`mem::replace(_)` to avoid needless clones](idioms/mem-replace.md)
### Design patterns

@ -19,7 +19,7 @@ fn a_to_b(e: &mut MyEnum) {
// we could just take a reference to `name` and clone that, but why pay an
// extra allocation for something we already have?
let have_name = match *e {
MyEnum::A(ref mut name, x) if x == 0 => {
MyEnum::A { ref mut name, x } if x == 0 => {
// this takes out our `name` and put in an empty String instead
// note that empty strings don't allocate
Some(mem::replace(name, "".to_string()))
@ -28,7 +28,7 @@ fn a_to_b(e: &mut MyEnum) {
_ => None
};
// the mutable borrow ends here, so we can change `e`
if let Some(name) = have_name { *e = MyEnum::B { name } }
if let Some(name) = have_name { *e = MyEnum::B { name: name } }
}
```
@ -80,4 +80,4 @@ like Indiana Jones, replacing the artifact with a bag of sand.
This gets rid of the [Clone to satisfy the borrow checker] antipattern in a
specific case.
[Clone to satisfy the borrow checker]()
[Clone to satisfy the borrow checker](TODO: Hinges on PR #23)

Loading…
Cancel
Save