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

Remove redundant reference in closure with deref coercion #307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/second-iter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>, _>(|node| &node);
self.next = node.next.as_ref().map::<&Node<T>, _>(|node| node);
```

See, map is a generic function:
Expand All @@ -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<T>, _>` says "it should return a
`&Node<T>`, 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<Node<T, _>>` to `Box<Node<T, _>>`, and
then to `&Node<T, _>`, 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. 😅
Expand Down