lifetime

Rust Higher-ranked callback bounds

A higher-ranked trait bound requires one callback to work with every eligible argument lifetime. A function can then borrow data created inside its own body and pass it to the callback, rather than asking the caller to choose that local lifetime. Expected output (not executed): Invoice 7 Why: The callback accepts the borrow created inside visit. Common mistake: Using one caller-selected lifetime for a reference to a value that does not exist until the function executes.

Formal Syntax & Structure

rust syntaxrust
F: for<'a> Fn(&'a str)

Working Example

Example Snippetrust
fn visit<F>(handle: F) where F: for<'a> Fn(&'a str) {    let record = String::from("Invoice 7");    handle(&record);}fn main() { visit(|s| println!("{s}")); }
Rust Reference