Back to course

Variables and Constants

Full Course: Zig Programming From Zero to Hero

Variables and Constants

In Zig, you must decide if a value is mutable or immutable at declaration.

  • const: Immutable. Once set, the value cannot change.
  • var: Mutable. The value can be changed later.

zig const constant_value: i32 = 5; var mutable_value: i32 = 10;

mutable_value = 20; // Allowed // constant_value = 10; // Error!

Zig requires all variables to be initialized. If you want to delay initialization, use undefined (but be careful!).

zig var x: i32 = undefined; x = 5;