Skip to content

Unexpected Destructuring Behaviour #19772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KilianHanich opened this issue Apr 26, 2024 · 4 comments
Closed

Unexpected Destructuring Behaviour #19772

KilianHanich opened this issue Apr 26, 2024 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@KilianHanich
Copy link

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

const assert = @import("std").debug.assert;

test "swapping" {
    var x: u32 = 3;
    var y: u32 = 5;
    
    x, y = .{ y, x };
    
    assert(y == 3);
    assert(x == 5);
}

This fails, x and y are both 5.

Expected Behavior

The test does not fail or gives a compile error that swapping values this way (like in Python) doesn't work, even if the syntax suggests it.

@KilianHanich KilianHanich added the bug Observed behavior contradicts documented or intended behavior label Apr 26, 2024
@nektro
Copy link
Contributor

nektro commented Apr 26, 2024

Duplicate #12064

@nektro
Copy link
Contributor

nektro commented Apr 26, 2024

https://ziglang.org/documentation/master/std/#std.mem.swap is recommended for this use case

eg std.mem.swap(u32, &x, &y);

@KilianHanich
Copy link
Author

Yeah ok, missed that issue.

@KilianHanich
Copy link
Author

PS: While yes, std.mem.swap is better for this use case, I constructed a minimal example here. You should be able to use for this rotating multiple values (e.g. x, y, z = .{y, z, x};) too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants