Skip to main content

Variables

Oxide distinguishes between immutable and mutable bindings using let and var.

Immutable Bindings

Use let to create immutable (non-reassignable) bindings:

let x = 10
let name: String = "Alice"
// x = 20 // Error: cannot assign to immutable variable

Transpiles to:

let x = 10;
let name: String = "Alice".to_string();

Mutable Bindings

Use var to create mutable (reassignable) bindings:

var count = 0
count = count + 1
count += 1

var message = "Hello"
message = "World"

Transpiles to:

let mut count = 0;
count = count + 1;
count += 1;

let mut message = "Hello".to_string();
message = "World".to_string();

Type Inference

Oxide infers types when possible:

let number = 42           // Inferred as Int
let pi = 3.14 // Inferred as Float64
let greeting = "Hello" // Inferred as String
let items = [1, 2, 3] // Inferred as Vec<Int>

Explicit type annotations are optional but can improve readability:

let count: Int = 100
let ratio: Float64 = 0.75

Constants

Use const for compile-time constants:

const MAX_SIZE: Int = 1024
const PI: Float64 = 3.14159265359

Transpiles to:

const MAX_SIZE: isize = 1024;
const PI: f64 = 3.14159265359;

Constants must:

  • Have an explicit type annotation
  • Be initialized with a constant expression
  • Use SCREAMING_SNAKE_CASE naming

Static Variables

Use static for global variables with a fixed memory location:

static COUNTER: Int = 0
static var MUTABLE_COUNTER: Int = 0 // Mutable static

Transpiles to:

static COUNTER: isize = 0;
static mut MUTABLE_COUNTER: isize = 0;
warning

Mutable statics require unsafe blocks to access in Rust. Use with caution.

Shadowing

Variables can be shadowed by declaring a new variable with the same name:

let x = 5
let x = x * 2 // Shadows previous x
let x = "hello" // Type can change when shadowing

Transpiles to:

let x = 5;
let x = x * 2;
let x = "hello";

Destructuring

Oxide supports pattern destructuring:

// Tuple destructuring
let (a, b) = (1, 2)

// Struct destructuring
let Point(x, y) = point

// With type annotations
let (first, second): (Int, String) = (1, "two")

Transpiles to:

let (a, b) = (1, 2);
let Point { x, y } = point;
let (first, second): (isize, String) = (1, "two".to_string());

Naming Conventions

Oxide uses camelCase for variables and functions:

let userName = "Alice"      // Correct: camelCase
let firstName = "Bob" // Correct: camelCase
// let user_name = "Carol" // Incorrect: snake_case

The formatter will warn about naming convention violations.

See Also