Skip to content

Zig Error: cannot assign to constant

error: cannot assign to constant means you tried to change something Zig treats as read-only. The obvious case is a name made with const. The less obvious ones trip people up for longer: function parameters, loop captures like |item|, a method’s self, and string literals are all constant too. This page goes through each one with its fix. For the other errors in this series, see Zig Errors Explained.

const std = @import("std");
pub fn main() void {
const score = 10;
score += 5;
std.debug.print("{d}\n", .{score});
}
main.zig:5:5: error: cannot assign to constant
score += 5;
^~~~~

The caret points at the thing you tried to change. =, +=, -=, *= and friends all count as assigning.

A constant is a value that can’t change after it’s created. In Zig, that’s not only names made with const: anything you haven’t explicitly asked to be changeable is read-only. That rule is why Zig code is easy to follow: if you see const, a parameter or a |capture|, you know the value stays put. The Assignment section of the language reference shows this exact error, and how var makes a value changeable.

The fix depends on what the constant is. Look at the line the caret points to and find which case below matches.

If it’s a name you made with const, and it really needs to change, make it a var with a type:

const std = @import("std");
pub fn main() void {
var score: u32 = 10;
score += 5;
std.debug.print("{d}\n", .{score});
}

Output:

15

The type matters: var score = 10; without one gives a different error, explained in variable of type ‘comptime_int’ must be const or comptime.

Function parameters are constants in Zig. Python, JavaScript and C all let you change them, so this one surprises a lot of people:

const std = @import("std");
fn addOne(n: i32) i32 {
n += 1;
return n;
}
pub fn main() void {
std.debug.print("{d}\n", .{addOne(4)});
}
main.zig:4:5: error: cannot assign to constant
n += 1;
^

Either return a new value directly (return n + 1;), or copy the parameter into a local var and change that:

const std = @import("std");
fn countdown(start: u32) void {
var n = start;
while (n > 0) : (n -= 1) {
std.debug.print("{d} ", .{n});
}
std.debug.print("go!\n", .{});
}
pub fn main() void {
countdown(3);
}

Output:

3 2 1 go!

Changing the caller’s variable is a different job: pass a pointer (n: *i32) and write n.* += 1. Zig Function Parameters covers both.

A loop capture like |price| is a copy of each item, and it’s constant:

const std = @import("std");
pub fn main() void {
var prices = [_]u32{ 10, 20, 30 };
for (prices) |price| {
price *= 2;
}
std.debug.print("{any}\n", .{prices});
}
main.zig:4:9: error: local variable is never mutated
var prices = [_]u32{ 10, 20, 30 };
^~~~~~
main.zig:4:9: note: consider using 'const'
main.zig:6:9: error: cannot assign to constant
price *= 2;
^~~~~

Two errors, one mistake. Even if you could change price, you’d only change a copy, and prices would stay the same (which is why Zig also says prices is never mutated). To change the items in place, loop over &prices and capture a pointer with |*price|:

const std = @import("std");
pub fn main() void {
var prices = [_]u32{ 10, 20, 30 };
for (&prices) |*price| {
price.* *= 2;
}
std.debug.print("{any}\n", .{prices});
}

Output:

{ 20, 40, 60 }

price.* means “the value price points to”. See Zig For Loops.

Fix 4: Take self as a pointer in methods that change it

Section titled “Fix 4: Take self as a pointer in methods that change it”

A method with self: Counter gets a constant copy of the struct, so it can’t change it:

const std = @import("std");
const Counter = struct {
n: u32,
fn bump(self: Counter) void {
self.n += 1;
}
};
pub fn main() void {
var c: Counter = .{ .n = 0 };
c.bump();
std.debug.print("{d}\n", .{c.n});
}
main.zig:6:13: error: cannot assign to constant
self.n += 1;
~~~~^~

Change the parameter to self: *Counter. You still call it as c.bump(); Zig passes &c for you:

const std = @import("std");
const Counter = struct {
n: u32,
fn bump(self: *Counter) void {
self.n += 1;
}
};
pub fn main() void {
var c: Counter = .{ .n = 0 };
c.bump();
c.bump();
std.debug.print("{d}\n", .{c.n});
}

Output:

2

The same error shows up when you change a field of a const struct (const p: Point = ...; p.x = 10;): make it a var. Zig Struct Methods explains self and *Self.

Fix 5: Copy a string literal before changing it

Section titled “Fix 5: Copy a string literal before changing it”

String literals like "cat" live in read-only memory, even if the name you put them in is a var:

const std = @import("std");
pub fn main() void {
var word = "cat";
word[0] = 'b';
std.debug.print("{s}\n", .{word});
}
main.zig:5:9: error: cannot assign to constant
word[0] = 'b';
~~~~^~~

var word only means you can point word at a different string. To change the letters, copy the literal into an array of your own with .*:

const std = @import("std");
pub fn main() void {
var word = "cat".*;
word[0] = 'b';
std.debug.print("{s}\n", .{&word});
}

Output:

bat

"cat".* copies the three bytes into a [3]u8 array that belongs to word. Zig Strings explains why literals are read-only.

  • A const you later decided to change. Fix 1.
  • Changing a parameter, especially with Python or C habits. Fix 2.
  • Doubling, trimming or fixing items in a for loop. Fix 3.
  • A method that updates a field. Fix 4.
  • Upper-casing or editing a string literal. Fix 5.

applyDiscount should take 10 off every price, so the program prints { 90, 40, 15 }. It has two “cannot assign to constant” problems. Fix them.

const std = @import("std");
fn applyDiscount(prices: []const u32) void {
for (prices) |p| {
p -= 10;
}
}
pub fn main() void {
var prices = [_]u32{ 100, 50, 25 };
applyDiscount(&prices);
std.debug.print("{any}\n", .{prices});
}

Hint: the slice must allow changes (drop const), and the loop needs a pointer capture.

Show solution
const std = @import("std");
fn applyDiscount(prices: []u32) void {
for (prices) |*p| {
p.* -= 10;
}
}
pub fn main() void {
var prices = [_]u32{ 100, 50, 25 };
applyDiscount(&prices);
std.debug.print("{any}\n", .{prices});
}

Output:

{ 90, 40, 15 }

[]const u32 is a slice you may only read, so it becomes []u32. A slice is already a view of the array, so the loop takes prices directly and |*p| gives a pointer to each item.

  • The error means you changed something read-only.
  • const names: switch to var with a type.
  • Parameters are constant: copy into a local var, or pass a pointer.
  • Loop captures are constant copies: use for (&items) |*item| and item.*.
  • Methods that change a struct need self: *Self. String literals need copying first ("cat".*).

Next up: unused function parameter, the error for an input your function never reads.

Quick check

1. Inside fn grow(size: u32) u32, the line size *= 2; fails. Why?
2. How do you change every item of var nums in a for loop?
3. var name = "zig"; name[0] = 'Z'; fails. What is the fix?