0PricingLogin
Zig Academy · Lesson

Default Field Values

Initialize fields without repetition.

Repeating Yourself

If most instances start the same way, retyping every field is tedious. Zig lets a field carry a default value built into the type.

Give a Field a Default

Add = value after the field type. Any instance that omits that field gets the default automatically.

const Config = struct {
    retries: u8 = 3,
};

All lessons in this course

  1. Declaring Structs and Fields
  2. Methods and the self Parameter
  3. Default Field Values
  4. Anonymous Structs and Tuples
← Back to Zig Academy