In the Limitations section, we explained that operators are overloaded to work with references and non-references.
If you wish to write generic functions which use operators with mixed reference and non-reference, it might get tricky at first to specify the trait bounds. This page should serve as a cookbook to help you.
The for<'a>
syntax is something called Higher-Rank Trait Bounds, often shortened as HRTB
operation | trait bound |
---|---|
T $op T
T: $Op<T, Output=T>
T $op &T
T: for<'a> $Op<&'a T, Output=T>
&T $op T
for<'a> &'a T: $Op<T, Output=T>
&T $op &T
for<'a> &'a T: $Op<&'a T, Output=T>