Skip to content

Commit 414dfb1

Browse files
committed
rustc: Improve long diagnostics for E0282
The new example uses a `char` iterator instead of `i32`, to avoid interplay between type inference and the default type for integer literals.
1 parent 6b292cd commit 414dfb1

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/librustc/diagnostics.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -375,39 +375,40 @@ by adding a type annotation. Sometimes you need to specify a generic type
375375
parameter manually.
376376
377377
A common example is the `collect` method on `Iterator`. It has a generic type
378-
parameter with a `FromIterator` bound, which is implemented by `Vec` and
379-
`VecDeque` among others. Consider the following snippet:
378+
parameter with a `FromIterator` bound, which for a `char` iterator is
379+
implemented by `Vec` and `String` among others. Consider the following snippet
380+
that reverses the characters of a string:
380381
381382
```
382-
let x = (1_i32 .. 10).collect();
383+
let x = "hello".chars().rev().collect();
383384
```
384385
385386
In this case, the compiler cannot infer what the type of `x` should be:
386-
`Vec<i32>` and `VecDeque<i32>` are both suitable candidates. To specify which
387-
type to use, you can use a type annotation on `x`:
387+
`Vec<char>` and `String` are both suitable candidates. To specify which type to
388+
use, you can use a type annotation on `x`:
388389
389390
```
390-
let x: Vec<i32> = (1_i32 .. 10).collect();
391+
let x: Vec<char> = "hello".chars().rev().collect();
391392
```
392393
393-
It is not necessary to annotate the full type, once the ambiguity is resolved,
394+
It is not necessary to annotate the full type. Once the ambiguity is resolved,
394395
the compiler can infer the rest:
395396
396397
```
397-
let x: Vec<_> = (1_i32 .. 10).collect();
398+
let x: Vec<_> = "hello".chars().rev().collect();
398399
```
399400
400401
Another way to provide the compiler with enough information, is to specify the
401402
generic type parameter:
402403
403404
```
404-
let x = (1_i32 .. 10).collect::<Vec<i32>>();
405+
let x = "hello".chars().rev().collect::<Vec<char>>();
405406
```
406407
407408
Again, you need not specify the full type if the compiler can infer it:
408409
409410
```
410-
let x = (1_i32 .. 10).collect::<Vec<_>>();
411+
let x = "hello".chars().rev().collect::<Vec<_>>();
411412
```
412413
413414
Apart from a method or function with a generic type parameter, this error can

0 commit comments

Comments
 (0)