I was messing around with rust when I wrote this code, it compiles but I don't understand what where T:for <'a> From<&'a str> means in this code. Does this mean T has to exist for as long as x of type &str exists where x is the thing which goes inside From T::from(x) .
My other question is , are these types of questions allowed on this forum? Since it doesn't appear to have a practical benefit and is more like a puzzle.
It's called a higher-ranked trait bound (HRTB), and it means that T must implement From<&str> for all possible choices of the lifetime of the reference.
Incidentally, the place you usually need HRTB is when you need to do something with a lifetime the caller can't name -- a lifetime that doesn't leave your function body. Like the borrow of a local variable.
// Works
fn greet<T>() -> impl Into<T>
where
T: for <'a> From<&'a str>,
{
let s = String::new();
let _ = T::from(&*s);
"hello"
}
// Fails
fn greet2<'a, T>() -> impl Into<T>
where
T: From<&'a str>,
{
let s = String::new();
let _ = T::from(&*s);
"hello"
}
In greet2, the caller chooses the lifetime -- it's a generic parameter on the function -- and the caller can only choose lifetimes longer than the function body.
You may have seen a HRTB before without realizing it. The Fn/FnMut/FnOnce traits have special sugar: Elided input lifetimes are actually higher-ranked.