Update docs for E0282 and E0283, as E0282 now doesn't trigger for collect

This commit is contained in:
Esteban Küber 2023-08-14 22:02:12 +00:00
parent 7313c10774
commit 4a0c676791
2 changed files with 59 additions and 12 deletions

View File

@ -3,7 +3,7 @@ The compiler could not infer a type and asked for a type annotation.
Erroneous code example:
```compile_fail,E0282
let x = "hello".chars().rev().collect();
let x = Vec::new();
```
This error indicates that type inference did not result in one unique possible
@ -11,21 +11,24 @@ type, and extra information is required. In most cases this can be provided
by adding a type annotation. Sometimes you need to specify a generic type
parameter manually.
A common example is the `collect` method on `Iterator`. It has a generic type
parameter with a `FromIterator` bound, which for a `char` iterator is
implemented by `Vec` and `String` among others. Consider the following snippet
that reverses the characters of a string:
In the example above, type `Vec` has a type parameter `T`. When calling
`Vec::new`, barring any other later usage of the variable `x` that allows the
compiler to infer what type `T` is, the compiler needs to be told what it is.
In the first code example, the compiler cannot infer what the type of `x` should
be: `Vec<char>` and `String` are both suitable candidates. To specify which type
to use, you can use a type annotation on `x`:
The type can be specified on the variable:
```
let x: Vec<char> = "hello".chars().rev().collect();
let x: Vec<i32> = Vec::new();
```
It is not necessary to annotate the full type. Once the ambiguity is resolved,
the compiler can infer the rest:
The type can also be specified in the path of the expression:
```
let x = Vec::<i32>::new();
```
In cases with more complex types, it is not necessary to annotate the full
type. Once the ambiguity is resolved, the compiler can infer the rest:
```
let x: Vec<_> = "hello".chars().rev().collect();

View File

@ -1,7 +1,51 @@
An implementation cannot be chosen unambiguously because of lack of information.
The compiler could not infer a type and asked for a type annotation.
Erroneous code example:
```compile_fail,E0283
let x = "hello".chars().rev().collect();
```
This error indicates that type inference did not result in one unique possible
type, and extra information is required. In most cases this can be provided
by adding a type annotation. Sometimes you need to specify a generic type
parameter manually.
A common example is the `collect` method on `Iterator`. It has a generic type
parameter with a `FromIterator` bound, which for a `char` iterator is
implemented by `Vec` and `String` among others. Consider the following snippet
that reverses the characters of a string:
In the first code example, the compiler cannot infer what the type of `x` should
be: `Vec<char>` and `String` are both suitable candidates. To specify which type
to use, you can use a type annotation on `x`:
```
let x: Vec<char> = "hello".chars().rev().collect();
```
It is not necessary to annotate the full type. Once the ambiguity is resolved,
the compiler can infer the rest:
```
let x: Vec<_> = "hello".chars().rev().collect();
```
Another way to provide the compiler with enough information, is to specify the
generic type parameter:
```
let x = "hello".chars().rev().collect::<Vec<char>>();
```
Again, you need not specify the full type if the compiler can infer it:
```
let x = "hello".chars().rev().collect::<Vec<_>>();
```
We can see a self-contained example below:
```compile_fail,E0283
struct Foo;