rust / expert
Snippet
Higher-Rank Trait Bounds (HRTB)
Higher-Rank Trait Bounds (HRTB) allow you to specify that a bound holds for all possible lifetimes. The 'for<'a>' syntax ensures the closure can handle a reference with any lifetime, rather than a specific one fixed at the call site.
snippet.rs
1
2
3
4
5
6
7
8
9
10
fn process_data<F>(data: &str, handler: F)whereF: for<'a> Fn(&'a str),{handler(data);}fn main() {process_data("expert", |s| println!("Processing: {}", s));}
Breakdown
1
for<'a> Fn(&'a str)
Specifies that the function F must implement the Fn trait for any lifetime 'a.