0Pricing
Mojo Academy · Lesson

Fields and the __init__ Method

Initialize a struct's state.

Fields Start Empty

Declaring fields gives a struct its shape, but it does not fill them in. You still need to set each field's starting value. 🧱

Meet __init__

The special method __init__ runs when you create an instance. Its job is to set up the struct's fields with real values.

fn __init__(out self):
    self.x = 0
    self.y = 0

All lessons in this course

  1. Defining a struct
  2. Fields and the __init__ Method
  3. Adding Methods to a Struct
  4. Structs vs Python Classes
← Back to Mojo Academy