Mojo types reference
Mojo is statically typed. Every value has a type known at compile time. The type system is designed to be simple and consistent, with a small set of core types that can be composed in powerful ways.
Numbers
Explore numbers on their dedicated page: Mojo numeric types.
| Type | What it is |
|---|---|
IntLiteral | Arbitrary-precision integer. Compile-time only. |
FloatLiteral | Arbitrary-precision float. Compile-time only. |
Int | Signed integer, system word size. Its own struct. |
SIMD | Fixed-size vector of numeric values. Mojo's numeric foundation. |
DType | Enumeration of numeric type identifiers. Used as a parameter. |
Scalar | Single numeric value. Alias for SIMD[dtype, 1]. |
UInt | Unsigned integer, system word size. A Scalar alias. |
Int8-Int256 | Fixed-width signed integers. Scalar aliases. |
UInt8-UInt256, Byte | Fixed-width unsigned integers. Scalar aliases. |
Float16-Float64 | IEEE 754 floating-point types. Scalar aliases. |
BFloat16 | Brain floating-point (16-bit). A Scalar alias. |
Float8_*, Float4_* | AI-optimized low-precision floats. Scalar aliases. |
Strings
| Type | What it is |
|---|---|
String | Heap-allocated, owned, mutable UTF-8 string. |
StringSlice | Non-owning view into UTF-8 string data. |
StaticString | Immutable slice of static string data. No heap allocation. |
StringLiteral | Compile-time string constant. |
Collections
| Type | What it is |
|---|---|
Tuple | Fixed-size, heterogeneous collection. |
List | Dynamically-sized, homogeneous sequence. |
Dict | Key-value mapping. |
Set | Unordered collection of unique values. |
Optional | A value that may or may not be present. |
Variant | A value that can be one of several types. |
Others
| Type | What it is |
|---|---|
Bool | Boolean value. Its own struct. |
Error | Represents a runtime error. |
Never | Return type for functions that don't return. |
None | The type with a single value, None. |
Bool
Bool represents a boolean value (True or False). It's its own
struct, backed by a 1-bit type, not a SIMD alias.
var flag = True
if flag:
print("yes")Using strings
Mojo has several string types for different ownership and lifetime needs.
String literals in source code (like "hello") are StringLiteral
values. They implicitly convert to String when needed.
All string types use UTF-8 encoding.
Mojo strings are Unicode-aware. Byte length, codepoint count, and grapheme count are distinct measurements:
var s: StringSlice = "café" # NFD: e + U+0301
print(s.byte_length()) # 5
print(len(s.codepoints())) # 4
print(s.count_graphemes()) # 4NFD normalization and emoji with modifiers or ZWJ sequences can widen these gaps further.
Tuples
A tuple is a fixed-size, heterogeneous collection. Elements can have different types, and the types are known at compile time.
var pair = (42, "hello")
var x = pair[0] # 42
var y = pair[1] # "hello"Tuples support unpacking:
var a: Int
var b: String
(a, b) = (42, "hello")Error
Error represents a recoverable runtime error. Functions that can
fail are marked raises and throw Error values:
def parse(s: String) raises -> Int:
...
raise Error("invalid input")Never
Never is the return type for functions that never return, such as
functions that always abort or loop forever:
def fatal(msg: String) -> Never:
print(msg)
abort()The compiler uses Never to prove that code after a call to such a
function is unreachable.
None
None is both a value and a type. The NoneType struct has exactly
one value: None. Functions that don't declare a return type
implicitly return None.
def greet(name: String):
print("hello", name)
# implicitly returns NoneWas this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!