Skip to content

Commit

Permalink
Fix a few nits with the replace idiom.
Browse files Browse the repository at this point in the history
Change the match guard to be part of the pattern.

Change `"".to_string()` to a more idiomatic `String::new()` to create a new `String` without memory allocation.
  • Loading branch information
CasualX authored and CasualX committed Oct 9, 2016
1 parent 9b289ac commit 69de235
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions idioms/mem-replace.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ 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: 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()))
Some(mem::replace(name, String::new()))
}
// nothing to do in all other cases
_ => None
Expand Down

0 comments on commit 69de235

Please sign in to comment.