`impl Trait`
impl Trait means 'some concrete type that implements this trait' without naming the type. In arguments it is a generic. In return position it is an opaque type.
Detailed Explanation
fn push(items: impl IntoIterator<Item = u32>) accepts anything that can become an iterator of u32: a Vec, an array, a range. The compiler treats this as a generic function. Each caller monomorphises a copy. You write impl Trait when the type parameter would appear only once and a name would add nothing.
fn evens(n: u32) -> impl Iterator<Item = u32> is the other meaning. The function returns one concrete type — usually an unnameable iterator adapter chain — and the signature only promises the trait. Callers cannot name the return type, cannot write it in a struct field without impl Trait in that position (or a dyn box), and cannot use methods that exist on the concrete type but not on the trait. That opacity is useful: you can change the adapter chain without breaking callers.
Argument-position impl Trait (APIT) is not object-safe sugar. It is static dispatch. Two parameters impl Read and impl Read are two different generic types; they may be different concrete types. If you need them to be the same type, write fn copy<R: Read>(a: R, b: R) so one parameter names both.
Return-position impl Trait (RPIT) must be a single concrete type on every path. if cond { iter::once(1) } else { vec![1, 2].into_iter() } will not compile even though both implement Iterator<Item = i32>. Use either::Either, box as Box<dyn Iterator>, or rewrite into one adapter chain.
Since Rust 1.75, impl Trait can appear in associated types and, in later editions, in more return positions on traits (RPITIT). The idea stays the same: hide a type behind a trait it implements.
When you write APIT: a single generic parameter used once, especially IntoIterator, AsRef<Path>, ToString. When you write RPIT: you are returning a closure or an iterator chain you do not want to name. When you need a name — a struct field, two functions sharing a type — use a generic parameter or dyn Trait.
Code Examples
fn sum(items: impl IntoIterator<Item = u32>) -> u32 {
items.into_iter().sum()
}
fn evens(n: u32) -> impl Iterator<Item = u32> {
(0..n).filter(|x| x % 2 == 0)
}
fn main() {
println!("{}", sum([1, 2, 3])); // prints: 6
let xs: Vec<u32> = evens(6).collect();
println!("{xs:?}"); // prints: [0, 2, 4]
}Common Gotchas
RPIT must be one concrete type
You cannot return two different iterator types from two branches. Box them as `dyn Iterator` or unify the chain.
Two `impl Trait` parameters are two types
`fn f(a: impl Clone, b: impl Clone)` does not require `a` and `b` to be the same. Name a type parameter when they must match.
Callers cannot name an RPIT type
You cannot write `let x: Filter<...> = evens(6)` from outside. Store it as `impl Iterator` in a function-local binding, or collect it.
APIT is not `dyn`
The function is still generic and will be monomorphised. Use `&dyn Trait` when you want one compiled copy.