Skip to content

Commit

Permalink
Merge pull request rust-unofficial#33 from partim/take-for-replace
Browse files Browse the repository at this point in the history
Mention Option::take() as an alternative to mem::replace()
  • Loading branch information
lambda-fairy authored Oct 10, 2016
2 parents 3bec875 + 9fd6a8f commit 9d95504
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions idioms/mem-replace.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ this case, we put in an empty `String`, which does not need to allocate. As a
result, we get the original `name` *as an owned value*. We can then wrap this in
another enum.

Note, however, that if we are using an `Option` and want to replace its
value with a `None`, `Option`’s `take()` method provides a shorter and
more idiomatic alternative.


## Advantages

Look ma, no allocation! Also you may feel like Indiana Jones while doing it.
Expand All @@ -78,6 +83,7 @@ However, in Rust, we have to do a little more work to do this. An owned value
may only have one owner, so to take it out, we need to put something back in –
like Indiana Jones, replacing the artifact with a bag of sand.


## See also

This gets rid of the [Clone to satisfy the borrow checker] antipattern in a
Expand Down

0 comments on commit 9d95504

Please sign in to comment.