r[subtype]
Subtyping and Variance
r[subtype.intro] Subtyping is implicit and can occur at any stage in type checking or inference.
r[subtype.kinds] Subtyping is restricted to two cases: variance with respect to lifetimes and between types with higher ranked lifetimes. If we were to erase lifetimes from types, then the only subtyping would be due to type equality.
Consider the following example: string literals always have 'static
lifetime. Nevertheless, we can assign s to t:
Since 'static outlives the lifetime parameter 'a, &'static str is a
subtype of &'a str.
r[subtype.higher-ranked] Higher-ranked function pointers and trait objects have another subtype relation. They are subtypes of types that are given by substitutions of the higher-ranked lifetimes. Some examples:
// Here 'a is substituted for 'static
let subtype: & = &;
let supertype: & = subtype;
// This works similarly for trait objects
let subtype: & = &|x| x;
let supertype: & = subtype;
// We can also substitute one higher-ranked lifetime for another
let subtype: & = &;
let supertype: &for<'c> fn = subtype;
r[subtyping.variance]
Variance
r[subtyping.variance.intro] Variance is a property that generic types have with respect to their arguments. A generic type's variance in a parameter is how the subtyping of the parameter affects the subtyping of the type.
r[subtyping.variance.covariant]
F<T>is covariant overTifTbeing a subtype ofUimplies thatF<T>is a subtype ofF<U>(subtyping "passes through")
r[subtyping.variance.contravariant]
F<T>is contravariant overTifTbeing a subtype ofUimplies thatF<U>is a subtype ofF<T>
r[subtyping.variance.invariant]
F<T>is invariant overTotherwise (no subtyping relation can be derived)
r[subtyping.variance.builtin-types] Variance of types is automatically determined as follows
| Type | Variance in 'a |
Variance in T |
|---|---|---|
&'a T |
covariant | covariant |
&'a mut T |
covariant | invariant |
*const T |
covariant | |
*mut T |
invariant | |
[T] and [T; n] |
covariant | |
fn() -> T |
covariant | |
fn(T) -> () |
contravariant | |
std::cell::UnsafeCell<T> |
invariant | |
std::marker::PhantomData<T> |
covariant | |
dyn Trait<T> + 'a |
covariant | invariant |
r[subtyping.variance.user-composite-types]
The variance of other struct, enum, and union types is decided by
looking at the variance of the types of their fields. If the parameter is used
in positions with different variances then the parameter is invariant. For
example the following struct is covariant in 'a and T and invariant in 'b, 'c,
and U.
use UnsafeCell;
r[subtyping.variance.builtin-composite-types]
When used outside of an struct, enum, or union, the variance for parameters is checked at each location separately.
# use UnsafeCell;