Define trait using HRTBs

You have to repeat non-supertrait bounds everywhere, so:

fn my_add<T: GenericNum>(a: T, b: T) -> T
where
    for<'a> &'a T: Add<Output = T> + Add<&'a T, Output = T>,
{
    a + &b + &b
}

See: https://github.com/rust-lang/rust/issues/20671

And also: 2089-implied-bounds - The Rust RFC Book

The RFC is accepted but not stable. I'm not sure what the outlook is, as it has negative consequences too.

3 Likes