https://doc.rust-lang.org/reference/trait-bounds.html#higher-ranked-trait-bounds
Higher-Rank Trait Bounds (HRTBs) in Rust are used when you need to express that a trait bound applies to all possible lifetimes, not just one. This is particularly useful when working with closures and function pointers that can be called with references of any lifetime. Here’s when you might use HRTBs:
Iterators: If you’re working with iterators and you want to specify that the item type of the iterator can be a reference with any lifetime, HRTBs allow you to do so. This ensures that the iterator can be used in a variety of contexts without lifetime conflicts
Generic Functions: In generic functions, when you want to accept a parameter that implements a trait for all lifetimes, HRTBs let you express this requirement. [This is common with traits like Fn, FnMut, and FnOnce](<https://doc.rust-lang.org/nomicon/hrtb.html>)
Here’s an example of HRTBs in use:
fn call_with_ref<F>(closure: F)
where
for<'a> F: Fn(&'a u8),
{
let value = 10;
closure(&value);
}
In this function, F is a type that implements Fn(&'a u8) for all choices of 'a, thanks to the HRTB for<'a>. This means closure can be called with a reference to a u8 with any lifetime, not just a specific one.
HRTBs are a more advanced feature of Rust’s type system, and they’re used when you need this level of flexibility with lifetimes and trait bounds.