66 lines
1.6 KiB
Plaintext
66 lines
1.6 KiB
Plaintext
---
|
|
title: Variables
|
|
description: Local state management with type inference and explicit mutability.
|
|
icon: Variable
|
|
---
|
|
|
|
In Mist, variables follow the language-wide `type name` convention. For local scope, the `var` keyword provides type inference, while explicit types can be used for clarity or strictness.
|
|
|
|
## Basic Declaration
|
|
|
|
Variables are declared using the `var` keyword for automatic type inference. Like Rust, variables are immutable by default.
|
|
|
|
```mist
|
|
var message = "Hello Mist";
|
|
var count = 42;
|
|
```
|
|
|
|
## Mutability
|
|
|
|
To allow a variable to be reassigned, use the `mut` modifier after the `var` keyword or the explicit type.
|
|
|
|
```mist
|
|
var mut score = 0;
|
|
score = 100;
|
|
|
|
f32 mut price = 19.99;
|
|
price = 14.99;
|
|
```
|
|
|
|
## Explicit Typing
|
|
|
|
```mist
|
|
u64 large_id = 1000234;
|
|
bool is_active = true;
|
|
```
|
|
|
|
## Arrays
|
|
|
|
```mist
|
|
var list = [1, 2, 3]; // Standard init
|
|
var zeros = [0; 10]; // Repeat notation: ten zeroes
|
|
```
|
|
|
|
## Pattern Destructuring
|
|
|
|
```mist
|
|
(i32, i32) (x, y) = get_coordinates();
|
|
(String, i32) (name, age) = get_user_info();
|
|
```
|
|
|
|
## Constants
|
|
|
|
Constants are immutable values evaluated at compile time with an explicit type.
|
|
|
|
```mist
|
|
const i32 MAX_RETRIES = 5;
|
|
const str* VERSION = "1.0.4";
|
|
```
|
|
|
|
## Key Characteristics
|
|
|
|
- **Predictable Order**: Whether using `var` or an explicit type, the name always follows the "source" of its data.
|
|
- **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.
|