Version: 1.1.0

Struct declarations

A struct declaration lists parameters, trait conformances, fields, and methods.

Essentials

  • The name is a type.
  • Traits in parentheses are promises.
  • Fields are var declarations.
  • Methods are nested functions.
  • An initializer is a method named __init__.

Example

struct Counter(Movable):
    var n: Int

    fn __init__(out self):
        self.n = 0

    fn inc(mut self):
        self.n += 1

Keep in mind

Field order is part of the layout. Do not reorder public fields of a type you have already shipped.