From 06ed203f9f68aa554888b3a082c26a2e6cfc4ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Gonz=C3=A1lez?= Date: Tue, 22 Aug 2023 20:52:34 +0200 Subject: [PATCH 1/2] fix: fix typos in mem-replace.md --- src/idioms/mem-replace.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/idioms/mem-replace.md b/src/idioms/mem-replace.md index ab2d431e..1045b08e 100644 --- a/src/idioms/mem-replace.md +++ b/src/idioms/mem-replace.md @@ -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`). @@ -69,7 +69,7 @@ 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, +`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. From 068cb30e1f6bccfade8dc8f896157a520dc31373 Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Tue, 22 Aug 2023 21:41:47 +0200 Subject: [PATCH 2/2] ci: appease linter --- src/idioms/mem-replace.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/idioms/mem-replace.md b/src/idioms/mem-replace.md index 1045b08e..28d0d27a 100644 --- a/src/idioms/mem-replace.md +++ b/src/idioms/mem-replace.md @@ -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 its 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.