Types
The primitive types, arrays, strings, lists, structs, enums, optionals, tuples, the unit type, and which types are Copy.
Primitive types
Section titled “Primitive types”| Type | Meaning |
|---|---|
i8, i16, i32, i64 |
Signed integers, two’s complement. i8 is Java’s byte, i16 is short, i32 is int, i64 is long |
u8, u16, u32, u64 |
Unsigned integers, for wire formats and bit work |
usize |
An index or length. 64-bit on every platform Calx targets, and 32-bit on wasm32, so conversions treat it as either (conversions) |
f32, f64 |
IEEE 754 binary32 and binary64: Java’s float and double |
bool |
true or false |
char |
An unsigned 16-bit UTF-16 code unit: Java’s char. Not a number: convert it to do arithmetic |
rune |
A Unicode scalar value: what iterating a string will give |
str |
A read-only view of UTF-8 text. String literals are str, or String where one is wanted (strings) |
Integers behave exactly as Java’s do, including overflow, division and shifts (integers). So do floats (floats). No type converts to another by itself (conversions).
Each integer type has MIN and MAX, except that usize.MAX isn’t available, since it depends on the target. The float types have NAN, INFINITY, NEG_INFINITY, MAX and MIN_POSITIVE.
fn main() { println("{i8.MIN} {i8.MAX} {u16.MAX} {i64.MAX}") println("{f32.MAX} {f64.MIN_POSITIVE}")}-128 127 65535 9223372036854775807 3.4028235E38 2.2250738585072014E-308
Arrays
Section titled “Arrays”[T; N] holds exactly N values of type T, where N is a constant usize. Arrays are values, stored where they’re declared. An array of Copy elements is Copy, and one of elements that own memory moves (ownership).
const SIDE: usize = 3
fn main() { var grid: [i32; SIDE] = [0; SIDE] // [x; n] is n copies of x grid[1] = 5 let primes = [2, 3, 5, 7] // the length comes from the literal var sum = 0 for p in primes { sum += p } println("{grid[1]} {sum}")}5 17
a[i]reads elementi, whereiis ausize. An index out of bounds panics. A constant index out of bounds is a compile error.for x in agoes over the elements in order (control flow).- An empty
[]needs its type from where it’s used.
Strings and lists
Section titled “Strings and lists”Stringowns UTF-8 text and can change it (strings).List<T>is a growable array that owns its elements (lists).Deque<T>is a growable ring, added to and taken from at both ends (deques).PriorityQueue<T>is a binary heap that gives its least element first (priority queues).Box<T>owns one value on the heap, so a type can hold itself through it (boxes).ArenaList<T>is a list whose memory is anArena’s, freed all at once with the arena’s other lists (arenas).Map<K, V>andSet<T>are hash tables (maps and sets), andSortedMap<K, V>andSortedSet<T>keep their keys in order (sorted maps).
Structs, enums and tuples
Section titled “Structs, enums and tuples”- A struct holds named fields (structs).
- An enum is one of several variants, each of which can carry data (enums).
- A tuple, such as
(i32, str), groups values by position (tuples). - Structs and enums can be generic, as in
Pair<i32, str>(generics). - An interface isn’t a type. It’s a bound on a generic parameter (interfaces).
Pointers
Section titled “Pointers”*T and *const T are raw pointers, for unsafe code and C functions only.
Optionals
Section titled “Optionals”T? is a T that may be absent: some(x) or none. There’s no null (optionals).
Copy and owning types
Section titled “Copy and owning types”A type is Copy when its values own no memory: the primitive types, enums without data, and arrays, tuples, optionals, structs and enums made only of Copy types. Assigning or passing one copies it. String, List and the types that hold them own memory, so they move instead, and are freed when their owner ends (ownership).
The unit type
Section titled “The unit type”() is the type of a function with no ->, and of statements. It has one value, also written (). A variable or parameter can’t be ().