Similar to struct, the definition for a trait type specifies named methods (and, possibly, fields). For example:

trait Id:
  id u32
  fn print()

However, trait types accomplish a very different purpose than struct types. Instead of specifying the complete field layout and behavior of concrete data, trait types abstractly specify a common pattern of shared behavior (methods) and state (fields) across multiple concrete types. By this understanding, the Id trait above expresses the idea that there are other types out there which define a print method (that accepts no arguments and return no values) as well as an unsigned integer field named id.

Because traits abstract over other types, their use differs from concrete types (like structs) in two important ways:

Traits make possible several valuable kinds of polymorphism, particularly: