← ClaudeAtlas

rust-callback-boundslisted

Use when you shape a callable in a public signature — a callback bound such as Fn(&T) -> K, a key projection, a visitor, or a struct field that holds a closure. Covers which bound accepts which closure, why for<'a> FnMut(&'a T) -> &'a K compiles today while a free type parameter cannot name the higher-ranked lifetime, HRTB as a no-escape promise, closure signature inference by syntactic position, the E0309 E0621 E0502 cascade that follows from hoisting a lifetime, and the cost table for a generic F field against Box<dyn Fn> and a bare fn pointer field. Triggers on "lifetime may not live long enough" from a closure, "one type is more general than the other", "borrowed data escapes outside of closure", "for<'a>", "hrtb", "sort_by_key", "callback returns a reference", "store a closure in a struct", "Box<dyn Fn>", "fn pointer field", "E0747", "E0562", "Arc<dyn Fn>", "reached the recursion limit while instantiating", or "function item types cannot be named directly".
po4yka/rust-skills · ★ 2 · AI & Automation · score 76
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