Skip to content

Commit cc00059

Browse files
committed
Rust Book: Generics: Resolving ambiguities
- Add a small section to generics.md to explain how ambiguities in type inference can be resolved using the ::<> syntax. - Add links from syntax-index.md and iterators.md. - Minor edits to iterators.md and structs.md.
1 parent f57a027 commit cc00059

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

src/doc/book/src/generics.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,51 @@ container types like [`Vec<T>`][Vec]. On the other hand, often you want to
140140
trade that flexibility for increased expressive power. Read about [trait
141141
bounds][traits] to see why and how.
142142

143+
## Resolving ambiguities
144+
145+
Most of the time when generics are involved, the compiler can infer the
146+
generic parameters automatically:
147+
148+
```rust
149+
// v must be a Vec<T> but we don't know what T is yet
150+
let mut v = Vec::new();
151+
// v just got a bool value, so T must be bool!
152+
v.push(true);
153+
// Debug-print v
154+
println!("{:?}", v);
155+
```
156+
157+
Sometimes though, the compiler needs a little help. For example, had we
158+
omitted the last line, we would get a compile error:
159+
160+
```rust,ignore
161+
let v = Vec::new();
162+
// ^^^^^^^^ cannot infer type for `T`
163+
//
164+
// note: type annotations or generic parameter binding required
165+
println!("{:?}", v);
166+
```
167+
168+
We can solve this using either a type annotation:
169+
170+
```rust
171+
let v: Vec<bool> = Vec::new();
172+
println!("{:?}", v);
173+
```
174+
175+
or by binding the generic parameter `T` via the so-called
176+
[‘turbofish’][turbofish] `::<>` syntax:
177+
178+
```rust
179+
let v = Vec::<bool>::new();
180+
println!("{:?}", v);
181+
```
182+
183+
The second approach is useful in situations where we don’t want to bind the
184+
result to a variable. It can also be used to bind generic parameters in
185+
functions or methods. See [Iterators § Consumers](iterators.html#consumers)
186+
for an example.
187+
143188
[traits]: traits.html
144189
[Vec]: ../std/vec/struct.Vec.html
190+
[turbofish]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect

src/doc/book/src/iterators.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ Here's the version that does compile:
135135
let one_to_one_hundred = (1..101).collect::<Vec<i32>>();
136136
```
137137

138-
If you remember, the `::<>` syntax allows us to give a type hint,
139-
and so we tell it that we want a vector of integers. You don't always
140-
need to use the whole type, though. Using a `_` will let you provide
141-
a partial hint:
138+
If you remember, the [`::<>` syntax](generics.html#resolving-ambiguities)
139+
allows us to give a type hint that tells the compiler we want a vector of
140+
integers. You don't always need to use the whole type, though. Using a `_`
141+
will let you provide a partial hint:
142142

143143
```rust
144144
let one_to_one_hundred = (1..101).collect::<Vec<_>>();

src/doc/book/src/structs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn main() {
134134
let age = 27;
135135
let peter = Person { name, age };
136136
137-
// Print debug struct
137+
// Debug-print struct
138138
println!("{:?}", peter);
139139
}
140140
```

src/doc/book/src/syntax-index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
<!-- Generics -->
126126

127127
* `path<…>` (*e.g.* `Vec<u8>`): specifies parameters to generic type *in a type*. See [Generics].
128-
* `path::<…>`, `method::<…>` (*e.g.* `"42".parse::<i32>()`): specifies parameters to generic type, function, or method *in an expression*.
128+
* `path::<…>`, `method::<…>` (*e.g.* `"42".parse::<i32>()`): specifies parameters to generic type, function, or method *in an expression*. See [Generics § Resolving ambiguities](generics.html#resolving-ambiguities).
129129
* `fn ident<…> …`: define generic function. See [Generics].
130130
* `struct ident<…> …`: define generic structure. See [Generics].
131131
* `enum ident<…> …`: define generic enumeration. See [Generics].

0 commit comments

Comments
 (0)