trait

Fn — repeatedly callable through shared access

Fn is suitable when the caller can invoke a callable through a shared reference. It does not imply absence of side effects, because interior mutability may be used. The captured factor is read but not moved out or directly mutated. Expected stdout (not executed): (8, 12) Common mistake: Assuming Fn guarantees a pure function. Static Rust 2024 example; not a verified ByteMind executable assessment.

Formal Syntax & Structure

rust syntaxrust
F: Fn(i32) -> i32

Working Example

Example Snippetrust
fn twice(f: impl Fn(i32) -> i32) -> (i32,i32) { (f(2), f(3)) }fn main() { let factor=4; println!("{:?}", twice(|n| n*factor)); }
Rust Reference