How does `for<>` (Higher-Ranked Trait Bounds) syntax differ from regular lifetime bounds in Rust?
Hi everyone! I've been writing Rust for a few months now, but I still get confused with complex lifetime issues. Recently I came across the `for<'a>` syntax and can't understand how it differs from regular lifetime bounds.
Here's an example that confused me. I have a function that takes a closure working with any lifetime:
```rust
fn example(f: F)
where
F: Fn(&str) -> &str
{
let s = String::from("hello");
println!("{}", f(&s));
}
```
But if I try to pass a closure that returns a reference to the argument, it works. And if I want the closure to accept a reference with any lifetime, I see this syntax in the documentation:
```rust
fn example(f: F)
where
F: for<'a> Fn(&'a str) -> &'a str
{
// ...
}
```
What's the difference?
No answers yet.
Log in to leave an answer
Blogs
PHP Application Security: Top 10 Vulnerabilities and Protection