Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Concepts

impl Trait in Rust

Reference for impl Trait in Rust: anonymous generics in arguments, opaque return types, returning closures and iterators, trait methods, use<..> captures and limits.

impl Trait names a type by the trait it implements instead of by its real name. Its meaning depends on where it appears. In argument position it is shorthand for an anonymous generic parameter: the caller picks the type. In return position it is an opaque type: the function body picks one concrete type, and callers see only that it implements the trait.

Return-position impl Trait is the usual way to return closures and long iterator chains, whose real types are unwritable or very long. It is resolved at compile time, so it is static dispatch with no allocation.

Rust
fn print_all(items: impl IntoIterator<Item = i32>) { /* ... */ }
fn evens(limit: u32) -> impl Iterator<Item = u32> { (0..limit).filter(|n| n % 2 == 0) }

Try it

Rust Argument and return position

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust impl Trait in traits and precise captures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Only one concrete type can be returned

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rules by position

Argument position

fn f(x: impl Trait) is almost the same as fn f<T: Trait>(x: T). The caller chooses the type and each call may use a different one. You cannot name the type, so you cannot use it elsewhere in the signature. Since Rust 1.63 you can still give explicit type arguments for the named generics when a function mixes both forms (the impl ones stay inferred).

Return position

The function body must return one concrete type on every path. Callers see only the trait, so they can call its methods and any auto traits (Send, Sync, Unpin) that leak through, but not inherent methods. To return different types, use an enum, a generic parameter, or Box<dyn Trait>.

In trait methods

fn items(&self) -> impl Iterator<Item = u32> in a trait declaration and its impls works since Rust 1.75. Each implementation may return a different concrete type. A trait using it in this way is not dyn compatible. async fn in traits is built on the same feature.

Capturing and use<..>

An opaque return type may hide borrowed lifetimes. In edition 2024 it captures all in-scope generic parameters and lifetimes automatically; in earlier editions only type parameters were captured unless you added + '_. + use<'a, T> (stable since Rust 1.82) states the captured set explicitly, and use<> captures nothing.

Usage notes

  • No let x: impl Trait. It is not allowed in local bindings or struct fields on stable Rust; use generics or a boxed trait object.
  • Prefer a named generic when a type must appear twice (fn same(a: impl Tr, b: impl Tr) allows two different types; fn same<T: Tr>(a: T, b: T) requires one).
  • Closures as arguments: f: impl Fn(i32) -> i32 is the most common modern form for callbacks.
  • Return a Box<dyn Fn> when different branches return different closures.
  • Opaque types hide implementation changes. You can swap the concrete iterator inside without breaking callers, which is useful for library API stability.
  • Trait bounds on return: -> impl Iterator<Item = T> + Clone adds more guarantees; extra traits you do not list are invisible.
  • Large signatures are clearer with named parameters and where clauses than with several impl arguments.