# Where is the official documentation on for<'a>

Google returns Higher-Rank Trait Bounds - The Rustonomicon , which is a bit sparse.

Is there an 'official + comprehensive' documentation on `for<'a>` ? There seems to be tidbits here & there, but I can't find a comprehensive + complete explaination.

There is also the language reference and the original RFC.

2 Likes

One somewhat important aspect seems to be documented in none of these resources listed in this thread so far: a bound `for<'a> (â€¦bound using 'a'â€¦)` only quantifies over those `'a` for which all the types / traits are valid for the lifetime `'a`. To give some example:

A bound `for<'a> Fn(&'a T)` (or equivalently written â€ś`Fn(&T)`â€ť) only quantifies over such `'a` that `&'a T` is valid, i.e. only those `'a` such that `T: 'a` is fulfilled. E.g. `for<'a> Fn(&'a &'b ())` only applies to those `'a` such that `'b: 'a`. You can observe this e.g. in something like

``````for<'a, 'b> Fn(&'a &'b (), &'b T) -> &'a T
``````

which is a bound that `|_, x| x` can implement because of the `'b: 'a` restriction which allows coercing `&'b T` into `&'a T`:

``````fn foo<T, F>(f: F)
where
F: for<'a, 'b> Fn(&'a &'b (), &'b T) -> &'a T
{}

fn bar<T>() {
foo::<T, _>(|_, x| x);
}
``````

This behavior, together with the fact that `rustc` itself doesnâ€™t quite fully reason about these implied bounds properly is also a source of a few soundness issues in the language currently (e.g. #25860, #84591). So donâ€™t feel too bad if you find these rules tricky or perhaps confusing, because `rustc` is actually feeling the same.

This knowledge is also important when designing unsafe APIs that rely on HRTBs for soundness. E.g. the issue Kimundi/owning-ref-rs#71 only exists because the type parameters `T` or `U` can limit the set of lifetimes that a HRTB `FnOnce(&T) -> &U` ranges over.

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.