compiler/rustc_error_codes/src/error_codes/E0562.md MARKDOWN 33 lines View on github.com → Search inside
1`impl Trait` is only allowed as a function return and argument type.23Erroneous code example:45```compile_fail,E05626fn main() {7    let count_to_ten: impl Iterator<Item=usize> = 0..10;8    // error: `impl Trait` not allowed outside of function and inherent method9    //        return types10    for i in count_to_ten {11        println!("{}", i);12    }13}14```1516Make sure `impl Trait` appears in a function signature.1718```19fn count_to_n(n: usize) -> impl Iterator<Item=usize> {20    0..n21}2223fn main() {24    for i in count_to_n(10) {  // ok!25        println!("{}", i);26    }27}28```2930See the [reference] for more details on `impl Trait`.3132[reference]: https://doc.rust-lang.org/stable/reference/types/impl-trait.html

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.