简体   繁体   English

在 Zig 中循环整数范围

[英]Looping over an integer range in Zig

Is a while-loop like this the idiomatic way to loop over an integer range in Zig?像这样的while循环是在Zig中循环整数范围的惯用方式吗?

var i: i32 = 5;
while (i<10): (i+=1) {
    std.debug.print("{}\n", .{i});
}

I first tried the python-like我首先尝试了类似python

for (5..10) |i| {
    // ....

but that doesn't work.但这不起作用。

zig has no concept of integer range loops but there's a hack by nektro which create a random []void slice, so you can iterate with for loop zig 没有整数范围循环的概念,但是nektro有一个 hack,它创建了一个随机[]void切片,因此您可以使用for循环进行迭代

const std = @import("std");

fn range(len: usize) []const void {
    return @as([*]void, undefined)[0..len];
}

for (range(10)) |_, i| {
    std.debug.print("{d}\n", .{i});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM