On this page
article
Chapter 3: Primitive Types
Integers
var a: i8 = -128;
var b: u8 = 255;
var c: i32 = -2_147_483_648; -- underscores for readability
var d: u64 = 18_446_744_073_709_551_615;
var e: isize = -1; -- pointer-sized signed
var f: usize = 0; -- pointer-sized unsigned
Integer overflow always panics with plain arithmetic. Use explicit operators for other behavior:
var x: u8 = 255;
var wrapped = x +% 1; -- wrapping: 0
var saturated = x +| 1; -- saturating: 255
-- x + 1 would panic
Floats
var a: f32 = 3.14;
var b: f64 = 2.718281828;
Booleans
var t: bool = true;
var f: bool = false;
if t and not f {
debug.print("yes\n");
}
Logical operators are words: and, or, not, xor. Bitwise operators are symbols: &, |, ~, ^.
Characters and Strings
var c: char = 'A'; -- Unicode scalar value
var byte: u8 = 65; -- a byte
-- strings are slices of bytes
var s: []const u8 = "hello";
var owned = try String::from(alloc, "hello"); -- heap-allocated UTF-8
Iterating a string:
for b in s.bytes() { -- b: u8, raw bytes }
for c in s.codepoints() { -- c: char, codepoints }
The Unit Type
fn doSomething(): void {
debug.print("done\n");
-- implicit return of void
}