From be8aa37af95e1591a06942c2aa8ce649bcb53642 Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Thu, 24 Oct 2024 02:17:25 +0300 Subject: [PATCH] remove redundant reference in closure with deref coercion --- src/second-iter.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/second-iter.md b/src/second-iter.md index 1a73ec7..70318a0 100644 --- a/src/second-iter.md +++ b/src/second-iter.md @@ -464,7 +464,7 @@ out, so we need to help it by being explicit. Thankfully this is pretty rare, in Just for completeness' sake, we *could* give it a *different* hint with the *turbofish*: ```rust ,ignore - self.next = node.next.as_ref().map::<&Node, _>(|node| &node); + self.next = node.next.as_ref().map::<&Node, _>(|node| node); ``` See, map is a generic function: @@ -477,8 +477,10 @@ The turbofish, `::<>`, lets us tell the compiler what we think the types of thos generics should be. In this case `::<&Node, _>` says "it should return a `&Node`, and I don't know/care about that other type". -This in turn lets the compiler know that `&node` should have deref coercion -applied to it, so we don't need to manually apply all those \*'s! +This in turn lets the compiler know that `node` should have deref coercion +applied to it, so we don't need to manually apply all those \*'s! In this way, +it automatically dereferences from `&Box>` to `Box>`, and +then to `&Node`, which is the type we specified using the turbofish syntax. But in this case I don't think it's really an improvement, this was just a thinly veiled excuse to show off deref coercion and the sometimes-useful turbofish. 😅