All checks were successful
Build and Deploy Quartz / build-and-deploy (push) Successful in 1m2s
73 lines
2.6 KiB
Markdown
73 lines
2.6 KiB
Markdown
|
||
> [!note]
|
||
>
|
||
> This content is generated by AI
|
||
|
||
The intuition behind the restriction from `Fn` to `FnMut` to `FnOnce` in Rust is about **how much control a closure needs over its captured environment**—specifically, how it uses the variables it captures.
|
||
|
||
# `Fn`: The Most Restrictive (Least Power)
|
||
|
||
- **What it means:** The closure only borrows variables immutably (`&T`).
|
||
- **Intuition:** It promises **not to change anything** it captures.
|
||
- **Analogy:** Like a `const` method in C++, or a function in Python/JS that only reads its arguments.
|
||
- **Example:**
|
||
|
||
```rust
|
||
let x = 5;
|
||
let f = || println!("{}", x); // Only reads x
|
||
```
|
||
|
||
- **Use case:** Can be called many times, safely, without side effects.
|
||
|
||
---
|
||
|
||
# `FnMut`: Medium Restriction (More Power)
|
||
|
||
- **What it means:** The closure can borrow variables mutably (`&mut T`).
|
||
- **Intuition:** It can **change** the captured variables, but doesn’t take ownership.
|
||
- **Analogy:** Like a non-const method in C++ that can modify members, or a function in Python/JS that mutates its arguments.
|
||
- **Example:**
|
||
|
||
```rust
|
||
let mut x = 5;
|
||
let mut f = || x += 1; // Modifies x
|
||
```
|
||
|
||
- **Use case:** Can be called multiple times, but may change state each time.
|
||
|
||
---
|
||
|
||
# `FnOnce`: Least Restrictive (Most Power)
|
||
|
||
- **What it means:** The closure **takes ownership** of captured variables (`T`).
|
||
- **Intuition:** It can **consume** (move out) the captured variables, so it can only be called once.
|
||
- **Analogy:** Like a function in C++/Python/JS that takes ownership and destroys or moves its arguments.
|
||
- **Example:**
|
||
|
||
```rust
|
||
let s = String::from("hello");
|
||
let f = || drop(s); // Moves s into the closure, can only call once
|
||
```
|
||
|
||
- **Use case:** Can only be called once, because it may destroy or move out its environment.
|
||
|
||
---
|
||
|
||
# **Why is Fn the tightest?**
|
||
|
||
- **Fn** is safest: it only reads, so it can be called any time, any number of times, even concurrently.
|
||
- **FnMut** is less safe: it can change things, so you need exclusive access.
|
||
- **FnOnce** is least safe: it can destroy or move things, so you can only call it once.
|
||
|
||
## **Intuitive Summary**
|
||
|
||
- **Fn:** "I just want to look, not touch."
|
||
- **FnMut:** "I want to touch and change, but I’ll give it back."
|
||
- **FnOnce:** "I want to take it away, maybe destroy it, so you can’t have it back."
|
||
|
||
## **Why does Rust do this?**
|
||
|
||
Rust’s closure traits are designed to **prevent bugs and data races** by making you specify exactly how your closure interacts with its environment. The tighter the restriction, the safer and more flexible the closure is.
|
||
|
||
Let me know if you want code examples comparing all three!
|