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
- id: C1 claim: The fn type is a function pointer and is not the same as the Fn closure trait. source: S1 evidence: "The fn type is called a function pointer"
- id: C2 claim: Function pointers implement Fn, FnMut, and FnOnce, so they can be passed where a closure is expected. source: S1 evidence: "Function pointers implement all three of the closure traits"
- id: C3 claim: Accepting only fn is useful when calling C functions, because C has no closures. source: S2 evidence: "C functions can accept functions as arguments, but C doesn't have closures."
Sources
- [S1] Advanced Functions and Closures. The Rust Project Developers. https://doc.rust-lang.org/book/ch20-04-advanced-functions-and-closures.html . License: MIT OR Apache-2.0. Accessed 2026-11-26.
- [S2] Advanced Functions and Closures (source). The Rust Project Developers. https://raw.githubusercontent.com/rust-lang/book/main/src/ch20-04-advanced-functions-and-closures.md . License: MIT OR Apache-2.0. Accessed 2026-11-26.
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
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.},
}
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.
- rust-advanced-functions.schema.json (schema; CC-BY-4.0)
- rust-advanced-functions.table.json (table; CC-BY-4.0)
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.