38. #[must_use] — Never Ignore What Matters
Rust’s #[must_use] attribute turns silent bugs into compile-time warnings — making sure important return values never get accidentally ignored.
The Problem: Silently Ignoring Results
Here’s a classic bug that can haunt any codebase:
| |
The function works fine, but the caller threw away useful information without even a whisper from the compiler.
The Fix: #[must_use]
Add #[must_use] to the function and the compiler has your back:
| |
Now if someone calls remove_expired_tokens(&mut tokens); without using the result, the compiler emits:
| |
Works on Types Too
#[must_use] isn’t just for functions — it shines on types:
| |
This is exactly why calling .map() on an iterator without collecting produces a warning — Map is marked #[must_use] in std.
Already in the Standard Library
Rust’s standard library uses #[must_use] extensively. Result, Option, MutexGuard, and many iterator adapters are all marked with it. That’s why you get a warning for:
| |
The iterator does nothing until consumed — and #[must_use] makes sure you don’t forget.
Quick Rules
Use #[must_use] when:
- A function returns a
Resultor error indicator — callers should handle failures - A function is pure (no side effects) — ignoring the return means the call was pointless
- A type is lazy (like iterators) — it does nothing until consumed
- The return value carries critical information the caller likely needs
The custom message string is optional but highly recommended — it tells the developer why they shouldn’t ignore the value.