Statements (v0.2.1)
Conditional and control statements for runtime execution.
Overview
Statements control what a function does step by step. Thrust uses explicit blocks for branches, loops, returns, and control statements.
Clear blocks make error handling easier to follow. They also make it easier to see where a function exits or changes state.
Prefer readable control flow over clever shortcuts, especially in code that handles pointers, files, or memory.
Primary source: syntax/statements/
Syntax Signatures
if ready {
return 0;
} else {
return 1;
}
break;
continue;
Behavior and Use
Use early returns for invalid input or error cases when that makes the main path clearer.
Avoid deeply nested conditionals when a small helper function would make the branch easier to understand.
Best Practices
- Return early when input is invalid.
- Keep branch bodies short and focused.
- Use break and continue when they make the loop clearer.
Blocks and Branches
Statements are grouped with braces so control flow is visible.
- if and else select runtime paths based on boolean expressions.
- A block creates a nested scope for local declarations.
- return exits the current function. Code after an unconditional terminator is treated as unreachable by later checks.
Loop Control
break and continue only make sense inside loops.
- break exits the current loop.
- continue skips the rest of the current iteration and proceeds with the next one.
- breakAll and continueAll are broader loop-control forms. Keep them rare and close to the loop logic they affect.
Example
fn classify(n: s32) s32 @public {
if n < 0 {
return -1;
}
if n == 0 {
return 0;
}
return 1;
}