lifetime
Rust Higher-ranked callbacks returning borrowed data
The callback can also return data tied to each individual invocation. A formatter that borrows a substring, for example, can expose the same input-output relationship for every borrow duration. Expected output (not executed): Red Why: The callback returns a slice tied to the input supplied by inspect. Common mistake: Interpreting the bound as a guarantee that every returned reference is static, or that the output must physically originate from the input.
Formal Syntax & Structure
rust syntaxrust
F: for<'a> Fn(&'a str) -> &'a strWorking Example
Example Snippetrust
fn first(s: &str) -> &str { s.split_whitespace().next().unwrap_or("") }fn inspect<F>(f: F) where F: for<'a> Fn(&'a str) -> &'a str { let label = String::from("Red pencil"); println!("{}", f(&label));}fn main() { inspect(first); }