VC: update docs
This commit is contained in:
@@ -4,26 +4,23 @@ description: Directing execution with expression-based logic, pattern matching,
|
||||
icon: Split
|
||||
---
|
||||
|
||||
Control flow in Mist provides a bridge between C-style procedural logic and Rust's expression-oriented design. Blocks, if statements, while/for/loop loops, and match expressions all support statement bodies.
|
||||
Control flow in Mist provides a bridge between C-style procedural logic and Rust's expression-oriented design. Conditions do not require parentheses, and blocks support expression bodies.
|
||||
|
||||
## Conditionals
|
||||
|
||||
The `if` statement evaluates a boolean expression:
|
||||
The `if` statement evaluates a boolean expression without parentheses:
|
||||
|
||||
```mist
|
||||
if (score > 50) {
|
||||
if score > 50 {
|
||||
println!("Pass");
|
||||
} else if (score == 50) {
|
||||
} else if score == 50 {
|
||||
println!("Borderline");
|
||||
} else {
|
||||
println!("Fail");
|
||||
}
|
||||
|
||||
// Single-statement body (no braces needed)
|
||||
if (is_active) println!("Running");
|
||||
|
||||
// Expression body (implicit return)
|
||||
let result = if (valid) { "ok" } else { "err" };
|
||||
let result = if valid { "ok" } else { "err" };
|
||||
```
|
||||
|
||||
## Match
|
||||
@@ -31,7 +28,7 @@ let result = if (valid) { "ok" } else { "err" };
|
||||
The `match` statement provides exhaustive pattern matching with support for multiple patterns per arm via `|`:
|
||||
|
||||
```mist
|
||||
match (task_state) {
|
||||
match task_state {
|
||||
TaskState::Pending => { println!("Queued"); }
|
||||
TaskState::Failed { reason, code } => {
|
||||
println!("Error {}: {}", code, reason);
|
||||
@@ -49,9 +46,9 @@ Patterns support destructuring, or-patterns, and wildcards:
|
||||
let x = 2;
|
||||
let result;
|
||||
|
||||
match (x) {
|
||||
match x {
|
||||
1 => { result = 10; }
|
||||
2 => { result = 20; }
|
||||
2 => result = 20,
|
||||
3 => { result = 30; }
|
||||
_ => panic!();
|
||||
}
|
||||
@@ -66,33 +63,39 @@ An infinite loop construct:
|
||||
```mist
|
||||
loop {
|
||||
println!("forever");
|
||||
if (done) break;
|
||||
if done { break; }
|
||||
}
|
||||
```
|
||||
|
||||
### For-In Loop
|
||||
|
||||
For loops iterate over an expression using the `pattern : expr` syntax:
|
||||
For loops iterate over an expression using `for pattern in expr` syntax:
|
||||
|
||||
```mist
|
||||
for (i : 0..4) {
|
||||
for i in 0 .. 4 {
|
||||
sum += i;
|
||||
}
|
||||
|
||||
// With pattern destructuring
|
||||
for ([k, _] : pairs) {
|
||||
for (k, _) in pairs {
|
||||
keys += k;
|
||||
}
|
||||
|
||||
// With range variable
|
||||
let r = 0 .. 5;
|
||||
for i in r {
|
||||
count++;
|
||||
}
|
||||
```
|
||||
|
||||
### While Loop
|
||||
|
||||
```mist
|
||||
while (count < 5) {
|
||||
while count < 5 {
|
||||
count++;
|
||||
}
|
||||
|
||||
while (active) {
|
||||
while active {
|
||||
wait_for_event();
|
||||
}
|
||||
```
|
||||
@@ -105,7 +108,8 @@ while (active) {
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Statement Bodies**: If, while, for, and loop branches can omit braces for single statements or expressions.
|
||||
- **Implicit Returns**: Expression bodies (without `;`) implicitly return their value.
|
||||
- **No Parentheses**: Conditions in `if`, `while`, and `match` do not require parentheses.
|
||||
- **Implicit Returns**: Expression bodies implicitly return their value.
|
||||
- **Pattern Integration**: Loops and match arms utilize Mist's pattern system for data destructuring.
|
||||
- **Multiple Patterns**: Match arms support `|` for matching multiple patterns.
|
||||
- **Expression `if`**: `if/else` blocks can be used as expressions.
|
||||
|
||||
@@ -12,7 +12,7 @@ Primary expressions are the starting point of any logic chain. These include lit
|
||||
|
||||
```mist
|
||||
let x = 42;
|
||||
let y = Math::PI;
|
||||
let pi = Math::PI;
|
||||
let coordinates = (10, 20, 30);
|
||||
```
|
||||
|
||||
@@ -72,9 +72,11 @@ let content = fs::read_to_string(path)?;
|
||||
|
||||
### Range Operators
|
||||
|
||||
Ranges use spaced `..` syntax:
|
||||
|
||||
```mist
|
||||
0..10 // exclusive range (0 to 9)
|
||||
0..=10 // inclusive range (0 to 10)
|
||||
0 .. 10 // exclusive range (0 to 9)
|
||||
0 ..= 10 // inclusive range (0 to 10)
|
||||
```
|
||||
|
||||
### Arrays
|
||||
@@ -108,6 +110,17 @@ let is_equal = (x == y);
|
||||
let complex = (a + b) * (c / d);
|
||||
```
|
||||
|
||||
## Closures
|
||||
|
||||
Closures use arrow syntax with optional type annotations:
|
||||
|
||||
```mist
|
||||
let add = (a, b) => a + b;
|
||||
let greet = (name str&) => {
|
||||
println!("Hello {}", name);
|
||||
};
|
||||
```
|
||||
|
||||
## Operator Table
|
||||
|
||||
| Category | Operators |
|
||||
@@ -126,3 +139,4 @@ let complex = (a + b) * (c / d);
|
||||
- **Macro Integration**: Macros use `!` as a postfix operation.
|
||||
- **Type Casting**: `as Type` provides explicit type conversion at the expression level.
|
||||
- **Error Propagation**: The `?` operator enables early returns for `Result`/`Option` types.
|
||||
- **Arrow Closures**: `(params) => expr` for concise anonymous functions.
|
||||
|
||||
@@ -4,15 +4,27 @@ description: Local state management with type inference and explicit mutability.
|
||||
icon: Variable
|
||||
---
|
||||
|
||||
Variables in Mist are declared with the `let` keyword. Like Rust, variables are immutable by default, and types are written after the name for scannability.
|
||||
Variables in Mist support two declaration styles: inferred typing with `let` and explicit type annotation. Like Rust, variables are immutable by default.
|
||||
|
||||
## Basic Declaration
|
||||
|
||||
Variables use `let` for automatic type inference. The type annotation is optional — when omitted, the compiler infers the type from the value.
|
||||
Use `let` for automatic type inference:
|
||||
|
||||
```mist
|
||||
let x = 42;
|
||||
let greeting = "Hello Mist";
|
||||
let s = "hello, world!";
|
||||
```
|
||||
|
||||
## Explicit Typing
|
||||
|
||||
Type annotations are placed before the name:
|
||||
|
||||
```mist
|
||||
i32 x = 42;
|
||||
bool is_active = true;
|
||||
str& name = "mist";
|
||||
f64 pi = 3.14;
|
||||
```
|
||||
|
||||
## Mutability
|
||||
@@ -24,14 +36,20 @@ let mut score = 0;
|
||||
score = 100;
|
||||
```
|
||||
|
||||
## Explicit Typing
|
||||
|
||||
Type annotations are placed after the name:
|
||||
Or with explicit typing:
|
||||
|
||||
```mist
|
||||
let id u64 = 1000234;
|
||||
let is_active bool = true;
|
||||
let name *str = "mist";
|
||||
i32 mut counter = 0;
|
||||
counter = 10;
|
||||
```
|
||||
|
||||
## Strings
|
||||
|
||||
String references use `str&` for a natural left-to-right read:
|
||||
|
||||
```mist
|
||||
str& name = "mist";
|
||||
str& greeting = "Hello";
|
||||
```
|
||||
|
||||
## Arrays
|
||||
@@ -43,16 +61,17 @@ let zeros = [0; 10]; // Repeat notation: ten zeroes
|
||||
|
||||
## Pattern Destructuring
|
||||
|
||||
Tuples are destructured using square brackets:
|
||||
Tuples are destructured using parentheses:
|
||||
|
||||
```mist
|
||||
let [a, b] = (10, "hello");
|
||||
let [a, [b, c]] = (1, (2, 3));
|
||||
let (a, b) = (10, "hello");
|
||||
let (a, (b, c)) = (1, (2, 3));
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Predictable Order**: The `let` keyword signals a binding, followed by the name, optional type, and optional value.
|
||||
- **Dual Declaration Styles**: `let` for inference, `Type name` for explicit typing.
|
||||
- **Safety First**: Immutability by default prevents accidental state changes.
|
||||
- **Zero-Cost Inference**: Type inference is handled entirely at compile time.
|
||||
- **Shadowing**: Mist supports variable shadowing within the same scope.
|
||||
- **`str&` Notation**: String references use postfix `&` for clear left-to-right reading.
|
||||
|
||||
Reference in New Issue
Block a user