Skip to content

Commit 97124d5

Browse files
committed
Update docs for E0282 and E0283, as E0282 now doesn't trigger for collect
1 parent 28df997 commit 97124d5

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

compiler/rustc_error_codes/src/error_codes/E0282.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,32 @@ The compiler could not infer a type and asked for a type annotation.
33
Erroneous code example:
44

55
```compile_fail,E0282
6-
let x = "hello".chars().rev().collect();
6+
let x = Vec::new();
77
```
88

99
This error indicates that type inference did not result in one unique possible
1010
type, and extra information is required. In most cases this can be provided
1111
by adding a type annotation. Sometimes you need to specify a generic type
1212
parameter manually.
1313

14-
A common example is the `collect` method on `Iterator`. It has a generic type
15-
parameter with a `FromIterator` bound, which for a `char` iterator is
16-
implemented by `Vec` and `String` among others. Consider the following snippet
17-
that reverses the characters of a string:
14+
In the example above, type `Vec` has a type parameter `T`. When calling
15+
`Vec::new`, barring any other later usage of the variable `x` that allows the
16+
compiler to infer what type `T` is, the compiler needs to be told what it is.
1817

19-
In the first code example, the compiler cannot infer what the type of `x` should
20-
be: `Vec<char>` and `String` are both suitable candidates. To specify which type
21-
to use, you can use a type annotation on `x`:
18+
The type can be specified on the variable:
2219

2320
```
24-
let x: Vec<char> = "hello".chars().rev().collect();
21+
let x: Vec<i32> = Vec::new();
2522
```
2623

27-
It is not necessary to annotate the full type. Once the ambiguity is resolved,
28-
the compiler can infer the rest:
24+
The type can also be specified in the path of the expression:
25+
26+
```
27+
let x = Vec::<i32>::new();
28+
```
29+
30+
In cases with more complex types, it is not necessary to annotate the full
31+
type. Once the ambiguity is resolved, the compiler can infer the rest:
2932

3033
```
3134
let x: Vec<_> = "hello".chars().rev().collect();

compiler/rustc_error_codes/src/error_codes/E0283.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
1-
An implementation cannot be chosen unambiguously because of lack of information.
1+
The compiler could not infer a type and asked for a type annotation.
22

33
Erroneous code example:
44

5+
```compile_fail,E0283
6+
let x = "hello".chars().rev().collect();
7+
```
8+
9+
This error indicates that type inference did not result in one unique possible
10+
type, and extra information is required. In most cases this can be provided
11+
by adding a type annotation. Sometimes you need to specify a generic type
12+
parameter manually.
13+
14+
A common example is the `collect` method on `Iterator`. It has a generic type
15+
parameter with a `FromIterator` bound, which for a `char` iterator is
16+
implemented by `Vec` and `String` among others. Consider the following snippet
17+
that reverses the characters of a string:
18+
19+
In the first code example, the compiler cannot infer what the type of `x` should
20+
be: `Vec<char>` and `String` are both suitable candidates. To specify which type
21+
to use, you can use a type annotation on `x`:
22+
23+
```
24+
let x: Vec<char> = "hello".chars().rev().collect();
25+
```
26+
27+
It is not necessary to annotate the full type. Once the ambiguity is resolved,
28+
the compiler can infer the rest:
29+
30+
```
31+
let x: Vec<_> = "hello".chars().rev().collect();
32+
```
33+
34+
Another way to provide the compiler with enough information, is to specify the
35+
generic type parameter:
36+
37+
```
38+
let x = "hello".chars().rev().collect::<Vec<char>>();
39+
```
40+
41+
Again, you need not specify the full type if the compiler can infer it:
42+
43+
```
44+
let x = "hello".chars().rev().collect::<Vec<_>>();
45+
```
46+
47+
We can see a self-contained example below:
48+
549
```compile_fail,E0283
650
struct Foo;
751

0 commit comments

Comments
 (0)