E0271trait

Type mismatch resolving trait bound

An associated type doesn't match what the bound requires.

Repro

fn first<I: Iterator<Item = i32>>(iter: I) -> Option<i32> { iter.into_iter().next() }
let v: Vec<&str> = vec!["a", "b"];
first(v.into_iter());   // E0271: expected `i32`, found `&str`

Why

The bound Iterator<Item = i32> requires Item = i32 exactly. The provided iterator's Item = &str. Common variants: - Async traits where Future::Output doesn't match the expected return type. - Stream<Item = T> with a different T. - Hand-written impls that pick the wrong type Output.

Fix

- Map the iterator to the expected type: v.into_iter().map(parse).collect::<Result<Vec<i32>, _>>()?. - Loosen the bound: Iterator<Item = T> with T generic on the function. - For futures, wrap or convert the value to the expected Output.

Patterns that solve this