# Swapping variables?

**URL:** <https://ziggit.dev/t/swapping-variables/18081>\
**Category:** Help\
**Tags:** language\
**Created:** [October 24, 2026, 2:33pm UTC](https://ziggit.dev/t/swapping-variables/18081 "2026-10-24T14:33:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuXYIO](https://ziggit.dev/user_avatar/ziggit.dev/suxyio/32/10366_2.png) [@SuXYIO](https://ziggit.dev/u/SuXYIO)\
**Post date:** [October 24, 2026, 2:33pm UTC](https://ziggit.dev/t/swapping-variables/18081/1 "2026-10-24T14:33:09Z")

</div>

Is there any syntax sugar for swapping variables in Zig? (e.g. `a, b = b, a`)  
I expected

```zig
const std = @import("std");

pub fn main() void {
    var a: i32 = 42;
    var b: i32 = 69;
    std.debug.print("{}, {}\n", .{ a, b });

    a, b = .{ b, a };
    std.debug.print("{}, {}\n", .{ a, b });
}

```

to work, but it outputs

```console
$ zig build run
42, 69
69, 69

```

which is confusing.

Explaining what’s going on here, and whether there’s a syntax sugar will be really appreciated!

---

<div class="post-metadata">

**Author:** ![abdullah-b-al](https://ziggit.dev/user_avatar/ziggit.dev/abdullah-b-al/32/4601_2.png) [@abdullah-b-al](https://ziggit.dev/u/abdullah-b-al)\
**Post date:** [October 24, 2026, 2:39pm UTC](https://ziggit.dev/t/swapping-variables/18081/2 "2026-10-24T14:39:09Z")

</div>

I don’t think there is syntax for swapping, but there is `std.mem.swap`.

As for

```zig
a, b = .{ b, a };

```

I believe it is the equivalent of:

```zig
a = b;
b = a;

```

That’s why the values are the same.

---

<div class="post-metadata">

**Author:** ![SuXYIO](https://ziggit.dev/user_avatar/ziggit.dev/suxyio/32/10366_2.png) [@SuXYIO](https://ziggit.dev/u/SuXYIO)\
**Post date:** [October 24, 2026, 3:27pm UTC](https://ziggit.dev/t/swapping-variables/18081/3 "2026-10-24T15:27:41Z")

</div>

Should’ve dug a little before posting,

> [@What output do you think this has, and what do you think it should have / wish it had?](https://ziggit.dev/t/what-output-do-you-think-this-has-and-what-do-you-think-it-should-have-wish-it-had/6877):
>
> Code: fn swap(a: anytype, b: anytype) void { if (@TypeOf(a) != @TypeOf(b)) @compileError("cannot swap two variables of different type"); a.\*, b.\* = .{ b.\*, a.\* }; } test "swap" { var a: i32 = 1; var b: i32 = 2; swap(&a, &b); try std.testing.expectEqualSlices(i32, &.{ 2, 1 }, &.{ a, b }); } Output:

And a bunch of issues mentioned this thingy, guess it’s expected behavior for now.  
Really wish this syntax could do the job though.

---

<div class="post-metadata">

**Author:** ![WeeBull](https://ziggit.dev/user_avatar/ziggit.dev/weebull/32/6828_2.png) [@WeeBull](https://ziggit.dev/u/WeeBull)\
**Post date:** [October 24, 2026, 4:10pm UTC](https://ziggit.dev/t/swapping-variables/18081/4 "2026-10-24T16:10:05Z")

</div>

Depending on the situation, I sometimes prefer to not swap the original variables but rather have swapped versions. For example:

```zig
    const reverse : bool = a < b;
    const x = if (reverse) b else a;
    const y = if (reverse) a else b;
    // Now do work knowing x and y are ordered versions of a and b.

```

My reasoning is that this can all be done in CPU registers, whereas actually swapping `a` and `b` will probably mean writing the swapped values back to memory. It might get optimised out if the optimiser can see all the scope but, if it can’t, it must play safe and write the swapped values back to memory.

[A very contrived example on Godbolt](https://godbolt.org/z/GPnnxeMsT)
