Comptime Iterations
Iteration variables are useable and they will be constant. You can use relevant variables to access iterated data.
A comptime iteration is defined like a typical range-based iteration. However, in addition, the const keyword must be included at the beginning of the definition. This declares that the iteration will be executed comptime. You cannot able to use continue or break keywords for compile-time iterations.
The types provided by the std/comptime package that can be used in iterations are explained in the package documentation.
Example to comptime iterations:
use "std/comptime"
struct MyStruct {
Foo: int
Bar: bool
Baz: str
}
fn main() {
const t = comptime::TypeOf(MyStruct)
const for _, field in t.Decl().Fields() {
println(field.Name())
}
}The example program above will prints names of MyStruct's fields. In compile-time, fields are iterated, and a new sub-scope is created for each iteration. This may increase your executable size because of code duplication; use it carefully.
Slice Literals
Slice literals are also supported by comptime iterations. All values of the slice literal should be constant (values supported by constant evaluation). When this slice is used for comptime iterations, it will iterate over each constant element sequentially.
For example:
fn main() {
const for _, p in ["hello", "comptime", "iterations"] {
println(p)
}
}In the example above, iteration will iterate each constant expression sequentially, from index 0 to index len-1. So output will be:
hello
comptime
iterations