Rust

impl Trait in Let Binding E0562 Fix

`impl Trait` names a concrete type without writing it, and the compiler only allows that shorthand in a function signature - as an argument type or as a return type. Write it in a `let` binding and the binding is rejected with E0562, however correct the value on the right-hand side is. The repair is to move the shorthand into a signature, name the concrete type, name a generic parameter, or switch to a trait object with `dyn`. This guide shows each failure, each allowed position, and each repair running.

Written December 1, 2026 · Tested with Rust 1.99.1 · 15 min read

By the end of this guide
  • The rule underneath: a signature shorthand, not a type you can name anywhere
  • How to read the diagnostic
  • The first failure: the index's let binding
  • Nesting does not hide it: tuple, Vec, reference, Box

Written December 1, 2026

Tested with Rust 1.99.1

What this guide is

Error E0562 means impl Trait is only allowed as a function return and argument type. The error codes index states that in one line, and its erroneous example is a let binding: let count_to_ten: impl Iterator<Item=usize> = 0..10;, commented "error: impl Trait not allowed outside of function and inherent method return types". Its advice is to make sure impl Trait appears in a function signature, and its corrected code moves the range into a function that returns it: fn count_to_n(n: usize) -> impl Iterator<Item=usize>.

The let binding is only the shape the index chose to show. The same shorthand draws the same code in a struct field, in a const or static type, inside a tuple, a Vec, a reference or a Box in a let binding, and in a closure's parameter or return annotation. One neighboring position is different: a type alias does not draw E0562 at all - it draws E0658, because impl Trait in type aliases is unstable. And the positions the rule allows really do compile: argument and return types of functions, of inherent methods, and of trait methods. Every one of those shapes is shown below.

Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.

The rule underneath: a signature shorthand, not a type you can name anywhere

The Rust Reference describes impl Trait as a way to specify unnamed but concrete types that implement a specific trait, and says it can appear in two sorts of places: argument position, where it acts as an anonymous type parameter to a function, and return position, where it acts as an abstract return type. Its Limitations section is exact: "impl Trait can only appear as a parameter or return type of a non-extern function. It cannot be the type of a let binding, field type, or appear inside a type alias."

That is why the value never matters. 0..10 really is an iterator, 42 really does implement Display, and a closure really does implement Fn - in each failing program below the right-hand side is fine. What is rejected is the annotation itself, because outside a signature there is no caller and no return for the shorthand to stand in for. A let binding needs a type the compiler can write down for that variable in that scope, and impl Iterator in that position is not one.

How to read the diagnostic

The header names the position, not just the code. In a let binding it reads "impl Trait is not allowed in the type of variable bindings"; in a field it reads "not allowed in field types"; in a const, "not allowed in const types"; in a closure, "not allowed in closure parameters" or "not allowed in closure return types". The span underlines the whole impl ... annotation.

Under the span sits the same note in every E0562 case shown here: "impl Trait is only allowed in arguments and return types of functions and methods". Most cases carry a second note, "see issue #63065" - the field, const, static and closure cases shown in full below do not carry that second note, while the let-binding cases do. There is no help block offering a rewrite: unlike some errors, E0562 does not propose a replacement line, because the right repair depends on what the binding was for - a signature, a concrete type, a generic, or a trait object.

The first failure: the index's let binding

The index's own example - a range bound to a variable with an impl Iterator annotation, then iterated:

Rust
fn main() {
    let count_to_ten: impl Iterator<Item=usize> = 0..10;
    for i in count_to_ten {
        println!("{}", i);
    }
}
Output
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
 --> fail_let_iterator.rs:2:23
  |
2 |     let count_to_ten: impl Iterator<Item=usize> = 0..10;
  |                       ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `impl Trait` is only allowed in arguments and return types of functions and methods
  = note: see issue #63065 <https://github.com/rust-lang/rust/issues/63065> for more information

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0562`.

Nothing is wrong with the range, the loop, or the printing. The one defect is the annotation: impl Iterator<Item=usize> in the type of a variable binding. The repairs for this exact program are shown running at the end of this guide.

Changing the trait does not help. The same position with three other trait shapes draws the identical header, note and issue note, with only the span and trait name changing:

  • let x: impl Display = 42; - impl Display in a let binding: E0562.
  • let x: impl Greet = 42;, where Greet is a trait defined in the same program and implemented for i32: E0562 - a custom trait is no exception.
  • let f: impl Fn(i32) -> i32 = |x| x + 1; - impl Fn in a let binding, with a closure as the value: E0562.

An unused binding fails too. let _x: impl Iterator<Item=usize> = 0..10; in a program that only prints a literal still draws E0562 as its only error - the annotation is rejected whether or not anything uses the variable, and in the single-defect programs in this guide no follow-on inference error appears after it: each aborts due to 1 previous error, and that error is E0562.

Nesting does not hide it: tuple, Vec, reference, Box

Putting the shorthand inside another type in the same binding changes nothing. Each of these let bindings draws E0562 with the header "not allowed in the type of variable bindings":

  • let pair: (impl Iterator<Item=usize>, i32) = (0..10, 1); - inside a tuple type: E0562.
  • let v: Vec<impl Iterator<Item=usize>> = vec![0..10]; - as a generic argument of Vec: E0562.
  • let x: &impl Iterator<Item=usize> = &(0..10); - behind a reference: E0562.
  • let x: Box<impl Iterator<Item=usize>> = Box::new(0..10); - inside Box: E0562, shown in full here because its repair is the dyn form below:
Rust
fn main() {
    let x: Box<impl Iterator<Item=usize>> = Box::new(0..10);
    println!("hi");
}
Output
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
 --> fail_let_box_impl.rs:2:16
  |
2 |     let x: Box<impl Iterator<Item=usize>> = Box::new(0..10);
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `impl Trait` is only allowed in arguments and return types of functions and methods
  = note: see issue #63065 <https://github.com/rust-lang/rust/issues/63065> for more information

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0562`.

The Box is not the problem - a boxed trait object is a real, nameable type. The problem is the word impl inside it: Box<dyn Iterator<Item=usize>> compiles and runs, as shown in the repairs.

The same shorthand in a field, a const, and a static

A struct field is the Reference's second named position, and it draws E0562 with its own header wording:

Rust
struct S { it: impl Iterator<Item=usize> }
fn main() { println!("hi"); }
Output
error[E0562]: `impl Trait` is not allowed in field types
 --> fail_struct_field.rs:1:16
  |
1 | struct S { it: impl Iterator<Item=usize> }
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0562`.

A field needs a stored type with a known layout, and the usual repairs here are the same families as for a binding: make the struct generic over the field type, or store a Box<dyn Iterator<Item=usize>>.

Item types behave the same way, each with its own header. A const item:

Rust
const X: impl Iterator<Item=usize> = 0..10;
fn main() { println!("hi"); }
Output
error[E0562]: `impl Trait` is not allowed in const types
 --> fail_const_item.rs:1:10
  |
1 | const X: impl Iterator<Item=usize> = 0..10;
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0562`.

A static item - static X: impl Iterator<Item=usize> = 0..10; - draws E0562 with the header "not allowed in static types", the same note, and no issue note. Neither item position is a function signature, so neither can use the shorthand; name the concrete type instead. Neighboring compile-time failures are a different guide: a constant whose evaluation itself fails is E0080, not a position error like this one.

Closures: the same code, different wording

A closure is not a function item, so annotating its signature with impl Trait draws E0562 as well - in the parameter:

Rust
fn main() {
    let f = |x: impl Iterator<Item=usize>| x.count();
    println!("{}", f(0..3));
}
Output
error[E0562]: `impl Trait` is not allowed in closure parameters
 --> fail_closure_param.rs:2:17
  |
2 |     let f = |x: impl Iterator<Item=usize>| x.count();
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0562`.

And in the return annotation - let f = || -> impl Iterator<Item=usize> { 0..3 }; - it draws E0562 with the header "not allowed in closure return types" and the same note. Closures take concrete parameter types, or no annotation and inference; if a signature with impl Trait is what the code needs, it needs to be a function, not a closure.

The boundary: a type alias is E0658, not E0562

The Reference lists type aliases alongside let bindings and fields as positions where impl Trait cannot appear - but the compiler does not report an alias with E0562. It reports an unstable feature, plus a second uncoded error:

Rust
type X = impl Iterator<Item=usize>;
fn main() { println!("hi"); }
Output
error[E0658]: `impl Trait` in type aliases is unstable
 --> fail_type_alias.rs:1:10
  |
1 | type X = impl Iterator<Item=usize>;
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information

error: unconstrained opaque type
 --> fail_type_alias.rs:1:10
  |
1 | type X = impl Iterator<Item=usize>;
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `X` must be used in combination with a concrete type within the same crate

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.

So an alias is not a repair for a failing let binding, and it is not this guide's error either: if the diagnostic says E0658 and "unstable", the impl Trait was in a type alias. On the stable toolchain used here the alias does not compile in any case - name the concrete type in the alias, or return impl Trait from a function instead.

The allowed positions: arguments, returns, inherent and trait methods

Every position the note names compiles and runs. As a function argument, impl Trait is the Reference's anonymous type parameter - the caller supplies a concrete type that satisfies the bounds:

Rust
use std::fmt::Display;
fn show(x: impl Display) { println!("{}", x); }
fn main() { show(42); }
Output
42

As a function return type it is an abstract return type - the function chooses the concrete type, and the caller uses only the trait's methods. That is the index's own repair shape, shown running in the repairs below: fn count_to_n(n: usize) -> impl Iterator<Item=usize> prints 0 through 9.

Inherent methods behave like free functions in both positions. A method fn show(&self, x: impl Display) -> impl Display { x } on a struct compiles, and calling it prints 42.

Trait methods compile in both positions too, in the probed forms. A trait declaration trait T { fn show(&self, x: impl Display); } compiles on its own, and with an implementation whose method uses the same argument form, calling it prints 42. A trait method returning impl Trait - trait T { fn get(&self) -> impl Iterator<Item=usize>; } - compiles as a declaration, and an implementation returning 0..3 runs, printing 0, 1 and 2. Other range errors are unrelated to any of this: an inclusive range with no end is E0586, a position error about a range, not about where a type shorthand may appear.

The repairs

The index's repair moves the value into a function that returns it, so the shorthand sits in return position, where it is allowed:

Rust
fn count_to_n(n: usize) -> impl Iterator<Item=usize> {
    0..n
}
fn main() {
    for i in count_to_n(10) {
        println!("{}", i);
    }
}
Output
0
1
2
3
4
5
6
7
8
9

The output is the ten values the original binding meant to hold. Choose this repair when the binding existed to hide a complicated concrete type - an iterator chain or a closure - behind the trait.

If the concrete type is short enough to write, name it. A range's type is Range<usize>:

Rust
use std::ops::Range;
fn main() {
    let count_to_ten: Range<usize> = 0..10;
    for i in count_to_ten {
        println!("{}", i);
    }
}
Output
0
1
2
3
4
5
6
7
8
9

The output is again 0 through 9. Choose this repair when the type is nameable and naming it costs nothing - it is the smallest change to the original line.

Where the value must be accepted from a caller rather than created in place, use a generic parameter, which is what argument-position impl Trait desugars to in a function signature:

Rust
fn sum<I: Iterator<Item=usize>>(it: I) -> usize { it.sum() }
fn main() { println!("{}", sum(0..10)); }
Output
45

The output is 45, the sum of 0 through 9. Choose this repair when the code should work for any type implementing the trait, chosen by the caller.

Where a named, owned trait object is what the binding actually wanted - one variable whose concrete type is chosen at runtime - use dyn inside the Box, not impl:

Rust
fn main() {
    let count_to_ten: Box<dyn Iterator<Item=usize>> = Box::new(0..10);
    for i in count_to_ten {
        println!("{}", i);
    }
}
Output
0
1
2
3
4
5
6
7
8
9

The output is 0 through 9. The same form repairs the nested Box failure above in place: Box<dyn Iterator<Item=usize>> compiles where Box<impl Iterator<Item=usize>> drew E0562, and a smaller boxed program that takes the first two values prints 0 then 1.

Fixes in reach order

First, if the binding was hiding a type that is awkward to write - an iterator, an adapter chain, a closure - move the expression into a function and put impl Trait in its return type. That is the index's repair, and it keeps the shorthand.

Second, if the concrete type is nameable, write it: Range<usize> for 0..10, and the concrete type in general for a field, a const, or a static. This is the only family that repairs the item and field positions, since those positions cannot become signatures.

Third, if the value should come from a caller, use a generic parameter - or, in a function signature only, argument-position impl Trait, which the Reference describes as syntactic sugar for one. Inside a let binding there is no such option.

Fourth, if the binding needs one name for different concrete types chosen at runtime, use a trait object: Box<dyn Trait> for an owned value, &dyn Trait for a borrowed one. Do not write impl inside the Box or behind the reference in a binding - both drew E0562 above.

Last, do not reach for a type alias. type X = impl Trait; is not E0562 but E0658 plus an "unconstrained opaque type" error, and it does not compile on the stable toolchain used here either.

Decision summary

E0562 fired for impl Trait in every let-binding shape tried - the index's impl Iterator example, impl Display, a custom trait, and impl Fn, an unused binding, and nested inside a tuple, a Vec, a reference and a Box - and in a struct field, a const type, a static type, and a closure's parameter and return annotations, each with a header naming its position. It did not fire for a type alias, which drew E0658 plus an uncoded "unconstrained opaque type" error instead, nor in any signature position tried: a function argument, a function return, an inherent method's argument and return, and a trait method's argument and return, in declaration and in implementation, all of which compiled. Compiling and running were: the index's return-position repair, a concrete Range<usize> annotation, a generic-parameter version, and Box<dyn Iterator> versions of the original binding and of the nested Box shape.

Checklist

  • The defect is the annotation's position, not the value: impl Trait outside a function or method signature is E0562, however well the value implements the trait.
  • A let binding, a field, a const and a static each draw E0562 with a header naming that position.
  • Nesting does not hide it: impl Trait inside a tuple, a Vec, a reference or a Box in a let binding is still E0562.
  • A closure is not a function: impl Trait in a closure parameter or return annotation is E0562.
  • A type alias is a different code: E0658, "unstable", plus an uncoded "unconstrained opaque type" error - not E0562, and not a repair.
  • Arguments and returns are the allowed positions - in free functions, inherent methods, and the trait methods probed, in declaration and implementation.
  • Repair by moving the shorthand into a return type, naming the concrete type, naming a generic parameter, or switching to Box<dyn Trait> / &dyn Trait.

Sources

  • Rust error codes index, E0562 - impl Trait is only allowed as a function return and argument type; the erroneous example is the let binding let count_to_ten: impl Iterator<Item=usize> = 0..10;, the advice is to make sure impl Trait appears in a function signature, and the corrected code is a function count_to_n returning impl Iterator<Item=usize>.
  • The Rust Reference, Impl trait type - impl Trait specifies unnamed but concrete types in argument position (an anonymous type parameter) and return position (an abstract return type); trait methods may use return-position impl Trait as an anonymous associated type; and the Limitations section states it can only appear as a parameter or return type of a non-extern function and cannot be the type of a let binding, a field type, or appear inside a type alias.

Keep Exploring