Passing Functions
Pass functions or closures to other functions with generics or fn types.
Example
fn double(x: i32) -> i32 { x * 2 }
fn apply(f: fn(i32) -> i32, v: i32) -> i32 { f(v) }
fn main() {
println!("{} {}", apply(double, 4), apply(|x| x + 1, 4));
}Result
8 5
Exercise
Function type.
f: (i32) -> i32
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.