]> git.proxmox.com Git - rustc.git/blame - src/librustc_error_codes/error_codes/E0284.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0284.md
CommitLineData
60c5eb7d
XL
1This error occurs when the compiler is unable to unambiguously infer the
2return type of a function or method which is generic on return type, such
3as the `collect` method for `Iterator`s.
4
5For example:
6
7```compile_fail,E0284
8fn foo() -> Result<bool, ()> {
9 let results = [Ok(true), Ok(false), Err(())].iter().cloned();
10 let v: Vec<bool> = results.collect()?;
11 // Do things with v...
12 Ok(true)
13}
14```
15
16Here we have an iterator `results` over `Result<bool, ()>`.
17Hence, `results.collect()` can return any type implementing
18`FromIterator<Result<bool, ()>>`. On the other hand, the
19`?` operator can accept any type implementing `Try`.
20
21The author of this code probably wants `collect()` to return a
22`Result<Vec<bool>, ()>`, but the compiler can't be sure
23that there isn't another type `T` implementing both `Try` and
24`FromIterator<Result<bool, ()>>` in scope such that
25`T::Ok == Vec<bool>`. Hence, this code is ambiguous and an error
26is returned.
27
28To resolve this error, use a concrete type for the intermediate expression:
29
30```
31fn foo() -> Result<bool, ()> {
32 let results = [Ok(true), Ok(false), Err(())].iter().cloned();
33 let v = {
34 let temp: Result<Vec<bool>, ()> = results.collect();
35 temp?
36 };
37 // Do things with v...
38 Ok(true)
39}
40```
41
42Note that the type of `v` can now be inferred from the type of `temp`.