From c0f97df1cc288806c01398769a5b1d6e6a9f7883 Mon Sep 17 00:00:00 2001 From: Meghan Date: Fri, 30 Apr 2021 21:47:47 -0700 Subject: [PATCH] std: add range(usize) --- lib/std/std.zig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/std/std.zig b/lib/std/std.zig index 940d1ab42eaa..9405fba43531 100644 --- a/lib/std/std.zig +++ b/lib/std/std.zig @@ -92,6 +92,18 @@ pub const x = @import("x.zig"); pub const zig = @import("zig.zig"); pub const start = @import("start.zig"); +/// Use this as a way to increment an index using a for loop. Works with both +/// runtime and comptime integers. +/// +/// ```zig +/// for (range(10)) |_, i| { +/// // 'i' will increment from 0 -> 9 +/// } +/// ``` +pub fn range(len: usize) []const u0 { + return @as([*]u0, undefined)[0..len]; +} + // This forces the start.zig file to be imported, and the comptime logic inside that // file decides whether to export any appropriate start symbols, and call main. comptime {