Skip to content
Tutorials Center
My learning

Topics

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.

Rust
fn double(x: i32) -> i32 { x * 2 }
let f: fn(i32) -> i32 = double;

Try it

Rust Dispatch tables and callbacks

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.

Rust fn items, sizes and method paths

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

Rust
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 fn pointer implements Fn, FnMut, FnOnce, Copy, Clone, Debug, Pointer, PartialEq, Eq, Hash and Send/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 with F: Fn therefore inlines better than code taking fn(...).
  • Mixed arrays: an array of different functions needs the pointer type, as in table above; 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, Ok and tuple-struct constructors can be passed wherever an fn is expected.
  • Option<fn(...)> is pointer-sized, because a function address is never null.
  • FFI: callbacks handed to C must be extern "C" fn (see extern).
  • No captured state: if a callback needs data, take a generic closure or a Box<dyn Fn>; see Fn traits.