primitive-type
Rust fn pointer type: store a callable function pointer
A function pointer stores a callable with a particular parameter list, return type and ABI. Named functions and non-capturing non-async closures can coerce to matching function pointers. Expected output (not executed): 12 7 Why: Both named functions have signatures matching the pointer type. Common mistake: A closure that captures a variable cannot coerce to an ordinary function pointer.
Formal Syntax & Structure
rust syntaxrust
fn(T) -> UWorking Example
Example Snippetrust
fn add_one(x: i32) -> i32 { x + 1 }fn double(x: i32) -> i32 { x * 2 }
fn main() { let operation: fn(i32) -> i32 = double; println!("{}", operation(6)); let operation: fn(i32) -> i32 = add_one; println!("{}", operation(6));}