Types (v0.2.1)
Primitive and built-in type forms used by Thrust.
Overview
Thrust types are written explicitly. Integers, floats, pointers, arrays, function references, and void all appear directly in source code.
This makes declarations easy to read. When a function takes u32, ptr[s32], or array[char; 64], the size and shape of the value are visible at the call boundary.
Use concrete types in public APIs unless there is a clear reason to introduce a type alias or a generic parameter.
Primary source: syntax/types/
Syntax Signatures
var signed: s32 = -1;
var unsigned: u64 = 1;
var decimal: f64 = 3.14;
var bufferPtr: ptr[u8] = nullptr;
var dynamic: array[s32] = [1, 2, 3];
var fixedValues: array[u16; 4] = fixed[0, 1, 2, 3];
Behavior and Use
Fixed-width integers are best for public APIs and binary formats because their size is part of the name.
Pointers and arrays mean different things. A pointer refers to memory elsewhere, while an array type describes a sequence of elements.
Best Practices
- Prefer explicit widths like `u32` or `s64` when size matters.
- Represent fixed-size protocol data with arrays, not dynamic buffers.
- Use aliases when a plain type needs a clearer name.
Scalar Types
Scalar values are stored directly and are usually passed by value.
- Signed integers use the s prefix, such as s8, s16, s32, and s64. Use them when negative values are part of the domain.
- Unsigned integers use the u prefix, such as u8, u16, u32, and u64. Use them for sizes, bit masks, and values that cannot be negative.
- Floating-point values use f32 or f64 in normal code. Wider or target-specific float forms should be kept near code that needs that representation.
- bool represents true or false values. Keep boolean names descriptive because the type alone does not explain the condition.
- char stores a byte-sized character value. Strings and UTF-8 data should still be treated as byte sequences unless a helper decodes code points.
Pointer and Array Types
Pointer and array syntax makes memory shape visible at the use site.
- ptr[T] points to a value of type T. The pointer may still be null or invalid, so code must check the value before dereferencing when the source is not trusted.
- ptr without a subtype is a raw pointer. Use it at FFI or allocator boundaries and cast back to a concrete pointer before reading typed values.
- array[T] describes a dynamic array form. Its length is not part of the type.
- array[T; N] describes a fixed-size array. The element count is part of the type and can be used by compile-time layout checks.
- CString is the builtin alias used for null-terminated character arrays passed to C-style APIs.
Aliases and Layout
Aliases and layout builtins help keep public declarations readable without hiding representation.
- type aliases give a name to an existing type. They do not create a separate runtime representation.
- sizeOf, alignOf, and typeWidth report layout information known to the compiler. Use them for constants and static checks instead of repeating numeric assumptions.
- ABI-oriented builtins such as abiSizeOf and abiAlignOf describe the representation required by the target ABI. Use them when interfacing with external code.
Example
fn main() s32 @public {
var values: array[s32; 3] = fixed[2, 4, 6];
var sum: s32 =
(deref values[0]) +
(deref values[1]) +
(deref values[2]);
return sum;
}