← All notes

Rust function pointers: pass fn values and return closures

Answer: This section explores advanced features related to functions and closures, including function pointers and returning closures.

Function pointers

The chapter covers advanced features of functions and closures, including function pointers and returning closures.

You can pass regular functions to functions, not only closures.

The fn type is called a function pointer, with a lowercase f, and should not be confused with the Fn closure trait.

Passing functions this way lets you use functions as arguments.

In the book's example, do_twice takes a function pointer and an i32, calls the function twice, and adds the results, so passing add_one prints The answer is: 12.

Unlike closures, fn is a type rather than a trait, so you name fn as the parameter type directly instead of introducing a generic type with an Fn bound.

Function pointers implement all three closure traits, Fn, FnMut, and FnOnce, so you can always pass a function pointer where a closure is expected.

Prefer a generic function with a closure trait so it can accept a closure or a function.

Accept only fn, not closures, when talking to external code without closures; C can accept functions as arguments and does not have closures.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Advanced Functions and Closures; The Rust Project Developers, Advanced Functions and Closures (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Advanced Functions and Closures; The Rust Project Developers, Advanced Functions and Closures (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers.

Cite this

Permanent ID: iil:note:0qt17yxq

iinventedlight. 2026. "Rust function pointers: pass fn values and return closures." iinventedlight Library, iil:note:0qt17yxq, version of 2026-11-26. https://iinventedlight.com/library/notes/rust-advanced-functions/. License: CC-BY-4.0.

@misc{iil_note_0qt17yxq,
  author = {{iinventedlight}},
  title = {{Rust function pointers: pass fn values and return closures}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-26},
  url = {https://iinventedlight.com/library/notes/rust-advanced-functions/},
  note = {Permanent ID iil:note:0qt17yxq. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-advanced-functions.md.