Contrl flow

This commit is contained in:
2026-05-12 03:30:23 +02:00
parent ecf4b16ec1
commit f6189e3fa2
2 changed files with 105 additions and 0 deletions
+93
View File
@@ -0,0 +1,93 @@
---
title: Control Flow
description: Directing execution with expression-based logic, pattern matching, and traditional loop structures.
icon: Split
---
Control flow in Mist provides a bridge between C-style procedural logic and Rust's expression-oriented design. While many structures can return values, they follow a strict syntax for blocks and statements.
## Conditionals
The `if` statement evaluates a boolean expression. It supports multiple `else if` branches and an optional `else` block.
```cpp
if (score > 50) {
println!("Pass");
} else if (score == 50) {
println!("Borderline");
} else {
println!("Fail");
}
```
## Match
The `match` statement provides exhaustive pattern matching. Currently, every match arm requires a block `{}` following the `=>` operator.
```cpp
match (task_state) {
TaskState::Pending => {
println!("Queued");
}
TaskState::Failed { reason, code } => {
println!("Error {}: {}", code, reason);
}
_ => {
println!("Other state");
}
}
```
## Loops
Mist supports both functional iteration and traditional low-level loop control.
### C-Style For Loop
For manual iteration control, Mist supports the standard three-part `for` loop: initialization, condition, and post-iteration statement.
```cpp
for (var mut i = 0; i < 10; i = i + 1;) {
println!("Index: {}", i);
}
```
### For-In Loop
The `for-in` loop iterates over collections or iterators using Mist's pattern matching system.
```cpp
for (var item in collection) {
process(item);
}
// Destructuring within the loop
for ((i32 x, i32 y) in coordinates) {
draw_point(x, y);
}
```
### While Loop
The `while` loop continues execution as long as the parenthesized expression evaluates to `true`.
```cpp
while (active) {
wait_for_event();
}
```
## Jump Statements
Execution flow can be interrupted or redirected using standard jump keywords.
- **`return`**: Exits the current function, optionally passing back a value.
- **`break`**: Terminates the innermost looping construct.
- **`continue`**: Skips the remainder of the current loop iteration and proceeds to the next.
## Key Characteristics
- **Pattern Integration**: Loops and match arms utilize Mist's pattern system, allowing for seamless data destructuring during iteration.
- **Explicit Scoping**: Match items currently require explicit blocks, ensuring clear boundaries for variable shadowing and local logic.
- **Familiar Iteration**: The inclusion of C-style `for` loops provides fine-grained control for performance-critical logic where simple iteration is insufficient.
- **Rust-Native Safety**: Despite the procedural syntax, these structures compile to safe Rust, maintaining exhaustive checking and memory safety.
+12
View File
@@ -36,6 +36,18 @@ u64 large_id = 1000234;
bool is_active = true; bool is_active = true;
``` ```
## Pattern Destructuring
Because variable declarations are patterns, you can destructure tuples or structures directly. This keeps data extraction clean and avoids manual indexing.
```rust
// Destructuring a tuple into local variables
(i32, i32) (x, y) = get_coordinates();
// Using 'var' within a pattern for inference
(String, i32) (name, age) = get_user_info();
```
## Constants ## Constants
Constants are immutable values that are evaluated at compile time. They require an explicit type and follow the `const` keyword. Constants are immutable values that are evaluated at compile time. They require an explicit type and follow the `const` keyword.