// Returns "some iterator yielding i32" — concrete type stays hidden.
fn evens_up_to(n: i32) -> impl Iterator<Item = i32> {
(0..n).filter(|x| x % 2 == 0)
}
// Closures have anonymous types — impl Fn is the way to return one.
fn adder(x: i32) -> impl Fn(i32) -> i32 {
move |y| x + y
}
fn main() {
for n in evens_up_to(10) {
print!("{n} "); // 0 2 4 6 8
}
println!();
let add5 = adder(5);
println!("{}", add5(3)); // 8
println!("{}", add5(10)); // 15
}
Create a free account and build your private vault. Share publicly whenever you want.