Variance describes what types are subtypes of other types and when a type can be used in place of a supertype (and viceversa).
For example, Turtle is a subtype of Animal and in Java you can pass Turtle to a function that accepts an Animal.

All types with generic parameters have a variance with respect to those parameters.
There are three kinds of variance:

  • covariant, a subtype can be used in place of the type. If Turtle <: Animal, then List<Turtle> <: List<Animal>
  • invariant, you must provide exactly the given type; neither subtype nor supertype is accepted
  • contravariant — a supertype can be used in place of the type. Function argument types are contravariant: a function that accepts Animal can be used where a function that accepts Turtle is expected, because it handles more

See also

References

  • J. Gjengset, Rust for Rustaceans: Idiomatic Programming for Experienced Developers. No Starch Press, 2021.