Variables & Types¶
Learn how to declare variables and use Desi's type system.
Declaring Variables¶
Immutable Variables: let¶
Use let for variables that won't change:
Trying to reassign a let variable is an error:
Mutable Variables: let mut¶
Use let mut when you need to modify a variable, and := to reassign it:
let mut counter = 0
counter := counter + 1 # ✅ OK
counter := counter + 1 # ✅ OK
print(counter) # Prints: 2
Prefer plain let
Use let by default and add mut only when mutation is needed. There is no
var keyword in Desi.
Type Annotations¶
Desi has strong static typing with inference. You can be explicit:
Or let Desi infer:
Basic Types¶
Numbers¶
| Type | Description | Example |
|---|---|---|
int |
Integer | 42, -17, 0 |
float |
Floating point | 3.14, -0.5 |
i32 |
32-bit signed | i32(100) |
i64 |
64-bit signed | i64(1000000) |
Strings¶
let greeting = "Hello, World!"
let name = "Desi"
let combined = greeting + " " + name # String concatenation
Booleans¶
Type Conversion¶
Convert between types explicitly:
let x: int = 42
let y: float = float(x) # int -> float
let z: str = str(x) # int -> str
let a: float = 3.7
let b: int = int(a) # float -> int (truncates to 3)
Constants¶
By convention, use UPPER_CASE for constants:
Multiple Declarations¶
Declare multiple variables:
Scope¶
Variables are scoped to their block:
def example():
let x = 10
if true:
let y = 20
print(x) # ✅ OK - x is in scope
print(y) # ✅ OK - y is in scope
print(x) # ✅ OK
# print(y) # ❌ Error - y is out of scope
Practical Example¶
def calculate_area():
let width: float = 10.5
let height: float = 20.0
let area = width * height
print("Width:")
print(width)
print("Height:")
print(height)
print("Area:")
print(area)
def main():
calculate_area()
Output:
Summary¶
| Keyword | Mutability | When to Use |
|---|---|---|
let |
Immutable | Default choice |
var |
Mutable | When value must change |
Next¶
Learn about Functions →