E0217: Callback escape
| Code | Category | Stability |
|---|---|---|
E0217 | Semantic | Permanent |
Explanation
A callback has no first-class value. Inside a body a fn parameter has exactly two uses: it is called with ordinary call rules, or it is forwarded as the argument to another fn parameter of the same type. Reading it anywhere else (binding it with let, returning it, comparing it, taking its address, storing it in an aggregate) is an escape and is rejected. A named function is likewise not a value outside a call or a fn argument position.
Likely cause
The body bound a callback parameter to a local, returned it, or used it in an expression, or a named function was mentioned without calling it. Call the callback, forward it directly as an argument, or pass the behavior again at each call that needs it.
Examples
Bind a callback parameter with let
Requires --preview fn_params.
fn apply(cb: fn(i32) -> i32, value: i32) -> i32 {
let kept = cb;
value
}
fn step(value: i32) -> i32 { value }
fn main() -> i32 { apply(step, 1) }
Call the callback instead
Requires --preview fn_params.
fn apply(cb: fn(i32) -> i32, value: i32) -> i32 { cb(value) }
fn main() -> i32 { 0 }