Skip to content

Syntax Guide ​

Project Links: GitHub

Overview ​

MaìLang uses a hybrid syntax style combining the best features of Rust, Python, and JavaScript.

Comments ​

// Single-line comment

/* 
   Multi-line comment
   Can span multiple lines
*/

/* /* Supports nesting */ */

Variables ​

Immutable let ​

let x = 42
let name: str = "MaìLang"

Mutable var ​

var count = 0
count = count + 1  // OK

Constants const ​

const PI = 3.14159

Basic Types ​

TypeDescriptionExample
int64-bit signed integer42
float64-bit float3.14
boolBooleantrue, false
strUTF-8 string"hello"
charUnicode character'A'
nullNull valuenull

String Interpolation ​

let name = "MaìLang"
let greeting = "Hello, {name}!"
let expr = "Result: {1 + 2}"

Operators ​

Arithmetic ​

OperatorDescriptionExample
+Addition1 + 2 = 3
-Subtraction5 - 3 = 2
*Multiplication4 * 3 = 12
/Division10 / 3 = 3
%Modulo10 % 3 = 1
**Power2 ** 3 = 8

Comparison ​

OperatorDescriptionExample
==Equal1 == 1 → true
!=Not equal1 != 2 → true
<Less than1 < 2 → true
>Greater than2 > 1 → true

Logical ​

OperatorDescriptionExample
&&Andtrue && false → false
||Ortrue || false → true
!Not!true → false

Control Flow ​

if-elif-else ​

if condition {
    // ...
} elif other {
    // ...
} else {
    // ...
}

for loop ​

for item in [1, 2, 3] {
    println(item)
}

for i in 0..10 {
    println(i)
}

while loop ​

var count = 0
while count < 10 {
    println(count)
    count += 1
}

match expression ​

let result = match x {
    0 => "zero",
    1..10 => "one to nine",
    _ => "other"
}

Functions ​

fn add(a: int, b: int) -> int {
    return a + b
}

// Default parameters are supported
fn greet(name: str) -> str {
    return "Hello, {name}!"
}

// Lambda
let square = fn(x) -> x * x

OOP ​

class Animal {
    let name: str
    
    fn init(name: str) {
        this.name = name
    }
    
    fn speak() -> str {
        return "{this.name} speaks"
    }
}

class Dog extends Animal {
    override fn speak() -> str {
        return "{this.name} barks!"
    }
}

trait Printable {
    fn to_string() -> str
    fn print() {
        println(this.to_string())
    }
}

Error Handling ​

fn divide(a: float, b: float) -> Result<float, str> {
    if b == 0.0 {
        return Err("Division by zero")
    }
    return Ok(a / b)
}

// Note: postfix `expr match { ... }` is supported
let value = match divide(10.0, 3.0) {
    Ok(v) => v,
    Err(e) => {
        println("Error: {e}")
        0.0
    }
}

Next Steps ​