]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0562.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0562.md
1 Abstract return types (written `impl Trait` for some trait `Trait`) are only
2 allowed as function and inherent impl return types.
3
4 Erroneous code example:
5
6 ```compile_fail,E0562
7 fn main() {
8     let count_to_ten: impl Iterator<Item=usize> = 0..10;
9     // error: `impl Trait` not allowed outside of function and inherent method
10     //        return types
11     for i in count_to_ten {
12         println!("{}", i);
13     }
14 }
15 ```
16
17 Make sure `impl Trait` only appears in return-type position.
18
19 ```
20 fn count_to_n(n: usize) -> impl Iterator<Item=usize> {
21     0..n
22 }
23
24 fn main() {
25     for i in count_to_n(10) {  // ok!
26         println!("{}", i);
27     }
28 }
29 ```
30
31 See [RFC 1522] for more details.
32
33 [RFC 1522]: https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md