Variables & Types
Learn about variables, type inference, and the type system in Sounio
Variables & Types
Sounio has a powerful type system with type inference, making code both safe and concise.
Variable Declaration
Immutable Variables (let)
By default, variables are immutable:
let x = 42 // Type inferred as i32
let name = "Alice" // Type inferred as String
let pi = 3.14159 // Type inferred as f64
Mutable Variables (var)
Use var for mutable variables:
var counter = 0
counter = counter + 1 // OK
let fixed = 10
fixed = 20 // Error: cannot assign to immutable variable
Type Annotations
You can explicitly annotate types:
let x: i32 = 42
let ratio: f64 = 0.5
let flag: bool = true
let message: String = "Hello"
Primitive Types
| Type | Description | Example |
|---|---|---|
i8, i16, i32, i64 | Signed integers | let x: i32 = -42 |
u8, u16, u32, u64 | Unsigned integers | let y: u32 = 42 |
f32, f64 | Floating point | let pi: f64 = 3.14 |
bool | Boolean | let flag: bool = true |
char | Unicode character | let c: char = 'A' |
String | Text string | let s = "hello" |
Compound Types
Tuples
let point = (10, 20)
let first = point.0 // 10
let second = point.1 // 20
Arrays
let numbers: [i32; 5] = [1, 2, 3, 4, 5]
let first = numbers[0] // 1
Vectors
let items: Vec<i32> = vec![1, 2, 3]
items.push(4) // [1, 2, 3, 4]
Type Inference
Sounio infers types from context:
let values = vec![1, 2, 3] // Vec<i32> inferred
let doubled = values.map(|x| x * 2) // Vec<i32> inferred
Units of Measure
Sounio supports dimensional types:
let mass: kg = 75.0
let height: m = 1.82
let bmi = mass / (height * height) // kg/m² inferred
See Units of Measure for more details.
Epistemic Types
For values with uncertainty:
let temp: Knowledge<celsius> = measure(
value: 23.5,
uncertainty: 0.2,
source: "thermometer"
)
See Knowledge Types for more details.