59 lines
1.4 KiB
Plaintext
59 lines
1.4 KiB
Plaintext
---
|
|
title: Variables
|
|
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.
|
|
|
|
## Basic Declaration
|
|
|
|
Variables use `let` for automatic type inference. The type annotation is optional — when omitted, the compiler infers the type from the value.
|
|
|
|
```mist
|
|
let x = 42;
|
|
let greeting = "Hello Mist";
|
|
```
|
|
|
|
## Mutability
|
|
|
|
To allow a variable to be reassigned, use `let mut`:
|
|
|
|
```mist
|
|
let mut score = 0;
|
|
score = 100;
|
|
```
|
|
|
|
## Explicit Typing
|
|
|
|
Type annotations are placed after the name:
|
|
|
|
```mist
|
|
let id u64 = 1000234;
|
|
let is_active bool = true;
|
|
let name *str = "mist";
|
|
```
|
|
|
|
## Arrays
|
|
|
|
```mist
|
|
let list = [1, 2, 3]; // Standard init
|
|
let zeros = [0; 10]; // Repeat notation: ten zeroes
|
|
```
|
|
|
|
## Pattern Destructuring
|
|
|
|
Tuples are destructured using square brackets:
|
|
|
|
```mist
|
|
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.
|
|
- **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.
|