Skip to content

Commit 7c989ca

Browse files
committed
cleanup turbofish example
1 parent c18cf98 commit 7c989ca

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

exercise-book/src/iterators.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,17 @@ Think of iterators as lazy functions - they only carry out computation when a *c
103103

104104
Iterators sometimes struggle to figure out the types of all intermediate steps and need assistance.
105105

106-
We can write
107-
108106
```rust
109-
let xs = v.iter()
110-
.map(|elem| elem * 2)
111-
.collect::<Vec<i32>>();
107+
let numbers: Vec<_> = ["1", "2", "3"]
108+
.iter()
109+
.map(|s| s.parse::<i32>().unwrap())
110+
// a turbofish in the `parse` call above
111+
// helps a compiler determine the type of `n` below
112+
.map(|n| n + 1)
113+
.collect();
112114
```
113115

114-
instead to avoid having a `xs: Vec<_> = ...`. This `::<SomeType>` syntax is called the [turbo fish operator](https://doc.rust-lang.org/book/appendix-02-operators.html?highlight=turbo%20fish#non-operator-symbols), and it disambiguates calling the same method with different output types, like `.collect::<HashSet<i32>>()` and `.collect::<Vec<i32>>()` (try it!)
116+
This `::<SomeType>` syntax is called the [turbo fish operator](https://doc.rust-lang.org/book/appendix-02-operators.html?highlight=turbo%20fish#non-operator-symbols), and it disambiguates calling the same method with different output types, like `.parse::<i32>()` and `.parse::<f64>()` (try it!)
115117

116118
### Dereferences
117119

0 commit comments

Comments
 (0)