diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 250252a77cbc..39ebcda4778c 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -375,39 +375,40 @@ 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 is implemented by `Vec` and -`VecDeque` among others. Consider the following snippet: +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: ``` -let x = (1_i32 .. 10).collect(); +let x = "hello".chars().rev().collect(); ``` In this case, the compiler cannot infer what the type of `x` should be: -`Vec` and `VecDeque` are both suitable candidates. To specify which -type to use, you can use a type annotation on `x`: +`Vec` and `String` are both suitable candidates. To specify which type to +use, you can use a type annotation on `x`: ``` -let x: Vec = (1_i32 .. 10).collect(); +let x: Vec = "hello".chars().rev().collect(); ``` -It is not necessary to annotate the full type, once the ambiguity is resolved, +It is not necessary to annotate the full type. Once the ambiguity is resolved, the compiler can infer the rest: ``` -let x: Vec<_> = (1_i32 .. 10).collect(); +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 = (1_i32 .. 10).collect::>(); +let x = "hello".chars().rev().collect::>(); ``` Again, you need not specify the full type if the compiler can infer it: ``` -let x = (1_i32 .. 10).collect::>(); +let x = "hello".chars().rev().collect::>(); ``` Apart from a method or function with a generic type parameter, this error can