Files
mist-website/content/docs/logic/variables.mdx
T
2026-06-28 22:45:01 +02:00

78 lines
1.6 KiB
Plaintext

---
title: Variables
description: Local state management with type inference and explicit mutability.
icon: Variable
---
Variables in Mist support two declaration styles: inferred typing with `let` and explicit type annotation. Like Rust, variables are immutable by default.
## Basic Declaration
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
To allow a variable to be reassigned, use `let mut`:
```mist
let mut score = 0;
score = 100;
```
Or with explicit typing:
```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
```mist
let list = [1, 2, 3]; // Standard init
let zeros = [0; 10]; // Repeat notation: ten zeroes
```
## Pattern Destructuring
Tuples are destructured using parentheses:
```mist
let (a, b) = (10, "hello");
let (a, (b, c)) = (1, (2, 3));
```
## Key Characteristics
- **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.