Rust Reference ยท Primitive Types
Rust function pointers: fn types
Reference for Rust function pointers: the fn(A) -> R type, coercion from functions and closures, dispatch tables, fn items vs pointers, and extern fn.
A function pointer has a type like fn(i32) -> i32: it holds the address of some function with that exact signature. Unlike closures, all function pointers with the same signature have the same type, so you can store them in arrays, struct fields and maps, and pass them across FFI boundaries.
Every named function has its own zero-sized "fn item" type, which coerces to the matching fn pointer type when needed. Closures that capture nothing coerce too. Closures that capture variables do not, because there is nowhere to keep the captured data.
fn double(x: i32) -> i32 { x * 2 } let f: fn(i32) -> i32 = double;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
fn(A, B) -> R // pointer type unsafe fn(A) -> R // pointer to an unsafe function extern "C" fn(A) -> R // C calling convention Option<fn(A)> // nullable function pointer for<'a> fn(&'a str) -> &'a str // higher-ranked (usually written fn(&str) -> &str)
| Part | Description |
|---|---|
A, B |
Parameter types (names are optional in the type). |
R |
Return type; omitted means (). |
| Qualifiers | unsafe, extern "ABI" are part of the type; a pointer to an extern "C" function is not the same type as a plain fn. |
Edge cases: a capturing closure assigned to a fn type fails with E0308 (expected fn pointer, found closure). Use a generic F: Fn(...) parameter or Box<dyn Fn(...)> instead.
Usage notes
- Traits: every
fnpointer implementsFn,FnMut,FnOnce,Copy,Clone,Debug,Pointer,PartialEq,Eq,HashandSend/Sync, so you can pass one anywhere a closure is expected. - Fn items versus pointers:
let f = square;has the zero-sized item type; the pointer type appears only when you ask for it (a type annotation, array of mixed functions, struct field). Generic code withF: Fntherefore inlines better than code takingfn(...). - Mixed arrays: an array of different functions needs the pointer type, as in
tableabove; otherwise each element has its own item type and the array does not compile. - Comparing function pointers can be unreliable: the compiler may merge identical functions or duplicate one across codegen units. Use them for calling, not identity.
- Enum constructors are functions:
Some,Okand tuple-struct constructors can be passed wherever anfnis expected. Option<fn(...)>is pointer-sized, because a function address is never null.- FFI: callbacks handed to C must be
extern "C" fn(seeextern). - No captured state: if a callback needs data, take a generic closure or a
Box<dyn Fn>; seeFn traits.
Related
Fn, FnMut and FnOnce,Closures,fn,extern,Trait objects and dispatch- Lessons: Closures, Trait objects, FFI