Use `for<'a>` in trait bounds to ensure a type implements a trait for all possible lifetimes.
Use the for<'a> syntax in a trait bound to specify that a type implements a trait for all possible lifetimes, not just a specific one.
fn process<F>(f: F)
where
F: for<'a> Fn(&'a str) -> &'a str
{
// f can accept a reference with any lifetime
}
This Higher-Ranked Trait Bound (HRTB) ensures the closure f works regardless of the lifetime of the input string slice.
Higher-Ranked Trait Bounds let you write functions that accept closures or types working with references of any lifetime. Instead of forcing a specific lifetime, you tell Rust the code must work for every possible lifetime. Think of it like a universal adapter that fits any power outlet, rather than one designed for a specific plug.