Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Invariance elsewhere

While behind a &mut is the most common place to first encounter invariance, it’s present elsewhere as well.

Cell<T> and RefCell<T> are also invariant in T.

Trait parameters are invariant too. As a result, lifetime-parameterized traits can be onerous to work with.

Additionally, if you have a bound like T: Trait<U>, U becomes invariant because it’s a type parameter of the trait. If your U resolves to &'x V, the lifetime 'x will be invariant too.

Associated types are also invariant.

-> impl Trait types are also invariant.