Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix typos in mem-replace.md #371

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/idioms/mem-replace.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum MyEnum {

fn a_to_b(e: &mut MyEnum) {
if let MyEnum::A { name, x: 0 } = e {
// this takes out our `name` and put in an empty String instead
// This takes out our `name` and puts in an empty String instead
// (note that empty strings don't allocate).
// Then, construct the new enum variant (which will
// be assigned to `*e`).
Expand Down Expand Up @@ -69,8 +69,8 @@ into our `MyEnum::B`, but that would be an instance of the
anti-pattern. Anyway, we can avoid the extra allocation by changing `e` with
only a mutable borrow.

`mem::take` lets us swap out the value, replacing it with it's default value,
and returning the previous value. For `String`, the default value is an empty
`mem::take` lets us swap out the value, replacing it with its default value, and
returning the previous value. For `String`, the default value is 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.

Expand Down
Loading