0Pricing
Learn Rust Coding · Lesson

Fn, FnMut, FnOnce

Closure traits.

Fn, FnMut, FnOnce is a free Learn Rust Coding lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Closure Traits

Every closure automatically implements one or more of three traits based on how it uses captured values:

  • Fn — borrows immutably.
  • FnMut — borrows mutably.
  • FnOnce — takes ownership, callable once.
fn main() {
    let x = 5;
    let show = || println!("x is {}", x); // implements Fn
    show();
    show();
}

Fn: Immutable Borrow

A closure that only reads captured values implements Fn. It can be called many times without changing anything.

fn main() {
    let greeting = String::from("Hi");
    let say = || println!("{}", greeting);
    say();
    say();
}

FnMut: Mutable Borrow

A closure that changes a captured value implements FnMut. The closure variable itself must be mut to call it.

fn main() {
    let mut count = 0;
    let mut increment = || {
        count += 1;
        println!("count {}", count);
    };
    increment();
    increment();
}

FnOnce: Takes Ownership

A closure that moves a captured value out of the environment implements FnOnce. It can only be called a single time.

fn main() {
    let name = String::from("Ada");
    let consume = move || {
        let owned = name;
        println!("consumed {}", owned);
    };
    consume();
}

The Trait Hierarchy

The traits form a hierarchy:

  • Every Fn is also FnMut and FnOnce.
  • Every FnMut is also FnOnce.

So Fn is the most flexible and FnOnce the least.

fn main() {
    let value = 42;
    let read = || value; // Fn, so also FnMut and FnOnce
    println!("{}", read());
}

Passing Fn to Functions

A function can accept a closure by specifying which trait it needs. Use generics with a trait bound.

fn apply<F: Fn(i32) -> i32>(f: F, x: i32) -> i32 {
    f(x)
}

fn main() {
    let double = |n| n * 2;
    println!("{}", apply(double, 9));
}

Accepting FnMut

If your function needs to mutate captured state, require FnMut and mark the parameter mut.

fn call_twice<F: FnMut()>(mut f: F) {
    f();
    f();
}

fn main() {
    let mut total = 0;
    call_twice(|| total += 1);
    // note: total is borrowed during the call
}

Accepting FnOnce

When a function only calls the closure once and may let it consume values, require FnOnce.

fn run_once<F: FnOnce() -> String>(f: F) {
    let result = f();
    println!("{}", result);
}

fn main() {
    let s = String::from("done");
    run_once(move || s);
}

The move Keyword

The move keyword forces a closure to take ownership of captured variables instead of borrowing.

This is essential when a closure must outlive the scope where it was created, such as in threads.

fn main() {
    let data = vec![1, 2, 3];
    let printer = move || println!("{:?}", data);
    printer();
}

How Rust Chooses the Trait

You do not pick the trait manually. The compiler decides based on the closure body:

  • Only reads → Fn
  • Mutates → FnMut
  • Moves out a captured value → FnOnce
fn main() {
    let mut log = Vec::new();
    let mut record = |x: i32| log.push(x); // FnMut
    record(1);
    record(2);
    println!("{:?}", log);
}

Choosing the Right Bound

As a rule:

  • Accept Fn when you only need to call and read.
  • Accept FnMut when the closure mutates state.
  • Accept FnOnce when calling once is enough.

Pick the most permissive bound that fits.

fn each<F: Fn(i32)>(items: &[i32], f: F) {
    for &i in items {
        f(i);
    }
}

fn main() {
    each(&[1, 2, 3], |n| println!("got {}", n));
}

Quick Check

Match a closure behavior to its trait.

Recap

The three closure traits:

  • Fn — borrows immutably, callable repeatedly.
  • FnMut — borrows mutably, can change state.
  • FnOnce — takes ownership, callable once.

The compiler infers the trait; move forces ownership capture.

fn main() {
    let base = 5;
    let adder = move |x: i32| x + base; // Fn, captures base by move
    println!("{}", adder(10));
}

Frequently asked questions

Is the “Fn, FnMut, FnOnce” lesson free?

Yes — the full text of “Fn, FnMut, FnOnce” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Fn, FnMut, FnOnce”?

Closure traits. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Fn, FnMut, FnOnce” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Functions
  2. Expressions and Statements
  3. Closures
  4. Fn, FnMut, FnOnce
← Back to Learn Rust Coding