Skip to content

Primitive types

API summary (generated from the Beans source by npm run coverage): 17 types · 2 instance methods.

A primitive type is the smallest kind of value Beans has. It is not made of other values. Every primitive is stored directly (unboxed) in the generated code, so it is cheap to use.

Every binding states its type. For example:

let x: int = 5
let ok: bool = true
let name: string = "beans"
TypeWhat it holdsSize
unitnothing; the empty value0 bytes
booltrue or false1 byte
int64-bit signed whole number8 bytes
i8 i16 i32 i64fixed-width signed whole numbers1, 2, 4, 8 bytes
uint64-bit unsigned whole number (the word type)8 bytes
u8 u16 u32 u64fixed-width unsigned whole numbers1, 2, 4, 8 bytes
byteanother name for u81 byte
float64-bit IEEE double8 bytes
f32 f64fixed-width floats4, 8 bytes
decimalbase-10 exact number32 bytes, align 16
stringimmutable UTF-8 text,
  • int is the canonical form of i64. When you write int you get a signed 64-bit whole number.
  • float is the same as f64.
  • byte is the same as u8.
  • uint and int are the word types. Beans has no usize or isize; use int for signed word-sized numbers and uint for unsigned ones.

unit is the empty value. It takes 0 bytes. A function that returns nothing returns unit. You rarely write it by hand.

bool is true or false, one byte.

decimal is a base-10 exact number, for values where a binary-float rounding error would be wrong, such as money. It takes 32 bytes and aligns to 16.

decimal is only present on targets that support it. It is refused on thumbv7em-none-eabi and riscv32-unknown-none-elf. Read Numbers and decimal for the rules.

string is immutable UTF-8 text. Once made, it does not change. See string for every method. For a growable, changeable byte buffer, see Bytes.

Even a literal number or string has methods. You can call a method right on a literal:

let a: int = (-5).abs()
let b: float = 3.7.round()
let c: Result<int> = "42".to_int()