Skip to content

Constructions

This page describes the main Zeen statements and expressions used to control program flow. They might be familiar for you from other languages.

Calls can be function calls, method calls or macro calls, and a call can be an expression that returns a value.
Syntax:

callee(args)

Possible callees:

// function call
foo("hello");
// compiler macro call
@println("hello");
// struct static call
Foo.new("hello")
// struct instance (method) call
foo_instance.func("hello")
// indirect call
let func: fn(i32) void = foo;
func(123);

Blocks can be a small execution context inside a statement, or an expression with a return value.
Syntax:

{
statements
}

The trailing semicolon after if / while / for / bare block is optional.

Conditional constructions are the statements that let you pick a branch based on a condition result.

Syntax:

if (condition) expression
if (condition) { statements }
if (condition) expression else expression
if (condition) { statements } else { statements }

The if construction is an expression, which means you can use it to assign a variable conditionally, or chain it to get the else if variation:

let a = 5;
let conditional = if (a == 5) 123 else 321;
if (conditional == 123) {
// ...
} else if (conditional == 321) {
// ...
} else {
// ...
}

Example:

fn main() {
let value = 123;
switch (value) {
1 => @println("this is one!"),
2 => @println("not exactly one"),
val if (val > 500) => @println("woah, very big!"),
_ if (0 == 1) => @println("wth is this"),
_ => @println("Seems like something else"),
};
}

Full description in the Switch topic.

Loops are constructions that repeat a block of statements while a condition holds.

Syntax:

while (condition) expression
while (condition) { statements }

Example:

fn main() {
let i = 0;
while (i < 10) {
@println("{}", i);
i += 1;
}
@println("loop ended");
}

Syntax:

for (varname : iterator) expression
for (varname : iterator) { statements }

Supported iterators types:

// integer signed types
i8 i16 i32 i64 isize
// integer unsigned types
u8 u16 u32 u64 usize
// array types
[N]T
// slice types
[]T
// structs with Iterator

Example:

for (i : 10) {
@println("{}", i);
}

Returns provided expression value for current function.
Syntax:

return; // empty/void return
return expression; // expression return

Example:

fn foo() i32 {
return 123 + 321 * 2;
}

Breaks current loop execution and returns to the branch after the loop.
Syntax:

break;

Example:

for (i : 10) {
if (i == 6)
break;
}

Skips current loop iteration and returns to the loop condition.
Syntax:

continue;