Skip to main content

Literals

Oxide supports various literal syntaxes for numbers, strings, and collections.

Numeric Literals

Integers

let decimal = 42
let negative = -17
let large = 1_000_000 // Underscores for readability
let hex = 0xFF // Hexadecimal
let octal = 0o77 // Octal
let binary = 0b1010 // Binary

Typed Literals

let byte: UInt8 = 255
let int32: Int32 = 42 // Explicit Int32
let uint64: UInt64 = 100 // Explicit UInt64
let int: Int = 50 // Platform-native signed

Floating Point

let pi = 3.14159
let scientific = 1.5e10 // Scientific notation
let negative = -0.5
let explicit: Float32 = 3.14 // Explicit Float32

String Literals

Basic Strings

let simple = "Hello, World!"
let withQuotes = "She said \"Hello\""
let withNewline = "Line 1\nLine 2"

Escape Sequences

EscapeCharacter
\\Backslash
\"Double quote
\'Single quote
\nNewline
\rCarriage return
\tTab
\0Null character
\xNNHex byte
\u{NNNN}Unicode
let tab = "Col1\tCol2"
let unicode = "Hello \u{1F44B}" // 👋
let hex = "\x48\x69" // "Hi"

Multi-line Strings

let multiline = """
This is a multi-line string.
It preserves indentation relative to
the closing quotes.
"""

String Interpolation

Display Interpolation ($)

Uses Display trait for formatting:

let name = "Alice"
let age = 30

// Variable interpolation
let greeting = "Hello, $name!"

// Expression interpolation
let message = "You are ${age * 12} months old"

// Escaped dollar sign
let price = "Cost: \$100"

Transpiles to:

let greeting = format!("Hello, {}!", name);
let message = format!("You are {} months old", age * 12);
let price = "Cost: $100";

Debug Interpolation (#)

Uses Debug trait for formatting:

let numbers = [1, 2, 3, 4, 5]
let maybe: Int? = 42

// Debug format
println!("Numbers: #numbers") // [1, 2, 3, 4, 5]
println!("Maybe: #maybe") // Some(42)

// Expression debug format
println!("Length: #{numbers.len()}")

// Mix Display and Debug
println!("Name: $name, Data: #numbers")

Transpiles to:

println!("Numbers: {:?}", numbers);
println!("Maybe: {:?}", maybe);

Multi-Dollar Strings

Control interpolation depth:

// $$ strings - single $ is literal
let template = $$"Use $variable for $$name"
// Result: "Use $variable for Alice"

// $$$ strings - $ and $$ are literal
let nested = $$$"Literal $, literal $$, interpolated $$$value"
// Multi-line multi-dollar
let sql = $$"""
SELECT * FROM users
WHERE name = '$username' -- literal $
AND id = $$id -- interpolated
"""

Character Literals

let letter = 'A'
let emoji = '😀'
let escape = '\n'
let unicode = '\u{1F600}'

Boolean Literals

let yes = true
let no = false

Collection Literals

Array/Vec Literals

// Vec (default for untyped)
let numbers = [1, 2, 3, 4, 5]

// Explicit array type
let array: [Int; 5] = [1, 2, 3, 4, 5]

// Empty collections
let empty: Vec<Int> = []
let emptyArray: [Int; 0] = []

Transpiles to:

let numbers = vec![1, 2, 3, 4, 5];
let array: [isize; 5] = [1, 2, 3, 4, 5];

Map Literals

let scores = ["Alice": 100, "Bob": 85, "Carol": 92]

// Explicit type
let config: Map<String, Int> = ["timeout": 30, "retries": 3]

// Empty map
let empty: Map<String, Int> = [:]

Transpiles to:

let scores = HashMap::from([
("Alice".to_string(), 100),
("Bob".to_string(), 85),
("Carol".to_string(), 92),
]);

Set Literals

// With explicit type annotation
let uniqueNumbers: HashSet<Int> = [1, 2, 3, 2, 1] // {1, 2, 3}

Transpiles to:

let unique_numbers: HashSet<isize> = HashSet::from([1, 2, 3, 2, 1]);

Tuple Literals

let pair = (1, "hello")
let triple = (1, 2.0, "three")
let nested = ((1, 2), (3, 4))

// Accessing elements
let first = pair.0
let second = pair.1

Range Literals

let range = 0..10        // [0, 10)
let closed = 0..=10 // [0, 10]
let from = 5.. // [5, ∞)
let to = ..5 // [0, 5)
let full = .. // Full range

Null Literal

let empty: Int? = null

Transpiles to:

let empty: Option<isize> = None;

Raw String Literals

For strings with many special characters:

let regex = r"\\d+\\.\\d+"
let path = r"C:\Users\name"

Note: Raw strings in Oxide work like Rust's r"..." strings - backslashes are literal.

Duration Literals

Oxide provides convenient duration syntax:

let timeout = 5.seconds
let delay = 100.millis
let hour = 1.minute
let long = 2.hours

Transpiles to:

let timeout = std::time::Duration::from_secs(5);
let delay = std::time::Duration::from_millis(100);
let hour = std::time::Duration::from_secs(60);
let long = std::time::Duration::from_secs(7200);

See Also