Skip to main content

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.

TypeWhat it is
IntLiteralArbitrary-precision integer. Compile-time only.
FloatLiteralArbitrary-precision float. Compile-time only.
IntSigned integer, system word size. Its own struct.
SIMDFixed-size vector of numeric values. Mojo's numeric foundation.
DTypeEnumeration of numeric type identifiers. Used as a parameter.
ScalarSingle numeric value. Alias for SIMD[dtype, 1].
UIntUnsigned integer, system word size. A Scalar alias.
Int8-Int256Fixed-width signed integers. Scalar aliases.
UInt8-UInt256, ByteFixed-width unsigned integers. Scalar aliases.
Float16-Float64IEEE 754 floating-point types. Scalar aliases.
BFloat16Brain floating-point (16-bit). A Scalar alias.
Float8_*, Float4_*AI-optimized low-precision floats. Scalar aliases.

Strings

TypeWhat it is
StringHeap-allocated, owned, mutable UTF-8 string.
StringSliceNon-owning view into UTF-8 string data.
StaticStringImmutable slice of static string data. No heap allocation.
StringLiteralCompile-time string constant.

Collections

TypeWhat it is
TupleFixed-size, heterogeneous collection.
ListDynamically-sized, homogeneous sequence.
DictKey-value mapping.
SetUnordered collection of unique values.
OptionalA value that may or may not be present.
VariantA value that can be one of several types.

Others

TypeWhat it is
BoolBoolean value. Its own struct.
ErrorRepresents a runtime error.
NeverReturn type for functions that don't return.
NoneThe 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())   # 4

NFD 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 None

Was this page helpful?