rust-callback-boundslisted
Install: claude install-skill po4yka/rust-skills
# Rust callback bounds
## Purpose
Two decisions, one subject: how to bound a callback parameter so the closures your callers write
are accepted, and how to store a callable in a struct field so your users can still name the type.
The sentence to get right: `for<'a> FnMut(&'a T) -> &'a K` **is legal and compiles today**. Only a
*separate* generic parameter, as in `FnMut(&T) -> K`, cannot name the higher-ranked lifetime. Do
not reach for a GAT, a macro crate, or `Box<dyn Fn>` when the callback returns a plain borrow of
its argument.
Every diagnostic, size, and allocation count below comes from rustc 1.97.0, edition 2024, on
aarch64-apple-darwin.
## Route the symptom to a section
| Symptom or task | Section |
| --- | --- |
| Choosing a bound and nothing has failed yet | [Pick the bound from the return type](#pick-the-bound-from-the-return-type) |
| `error: lifetime may not live long enough`, `return type of closure is &'2 ...` | [A free type parameter cannot name 'a](#a-free-type-parameter-cannot-name-a) |
| Callback must return a borrow of its argument | [Return a borrow of the argument](#return-a-borrow-of-the-argument) |
| `error[E0521]: borrowed data escapes outside of closure` | [for<'a> is a no-escape promise](#fora-is-a-no-escape-promise) |
| `error[E0308]: ... one type is more general than the other` | [Closure inference is positional](#closure-inference-is-positional) |
| `error[E0282]: type annotations needed` on a closure argument | [Closure inference is positio