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 callfoo("hello");
// compiler macro call@println("hello");
// struct static callFoo.new("hello")
// struct instance (method) callfoo_instance.func("hello")
// indirect calllet func: fn(i32) void = foo;func(123);Blocks
Section titled “Blocks”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
Section titled “Conditional Constructions”Conditional constructions are the statements that let you pick a branch based on a condition result.
If / If-Else
Section titled “If / If-Else”Syntax:
if (condition) expressionif (condition) { statements }
if (condition) expression else expressionif (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 { // ...}Switch
Section titled “Switch”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) expressionwhile (condition) { statements }Example:
fn main() { let i = 0;
while (i < 10) { @println("{}", i);
i += 1; }
@println("loop ended");}Syntax:
for (varname : iterator) expressionfor (varname : iterator) { statements }Supported iterators types:
// integer signed typesi8 i16 i32 i64 isize
// integer unsigned typesu8 u16 u32 u64 usize
// array types[N]T
// slice types[]T
// structs with IteratorExample:
for (i : 10) { @println("{}", i);}Program Flow Controllers
Section titled “Program Flow Controllers”return
Section titled “return”Returns provided expression value for current function.
Syntax:
return; // empty/void returnreturn expression; // expression returnExample:
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;}continue
Section titled “continue”Skips current loop iteration and returns to the loop condition.
Syntax:
continue;