IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

Mojo types & literals cheat sheet

Numbers, SIMD, conversions, and how to write values.

SIMD is the foundation

# Every fixed-width number is a 1-lane SIMD
# Float32 = Scalar[DType.float32]
# = SIMD[DType.float32, 1]

var v = SIMD[DType.float32, 4](1.0, 2.0, 3.0, 4.0)
var d = v * 2.0 # [2, 4, 6, 8], all lanes
v[0] = 5.0 # write one lane
print(v.reduce_add()) # sum of lanes (14.0)

Width must be a power of two and is part of the type; its parameter type is SIMDLength.

DType: what a lane holds

SIMD[DType.float32, 4] # DType picks the lane type
Scalar[DType.int] # == Int

Names mirror the types: DType.float32Float32, DType.int8Int8, DType.boolBool. A DType is a name, not a type. It parameterizes SIMD, which stores the data.

Integers

var n = 42 # Int: machine width
var u: UInt = 42 # machine width
var small: UInt8 = 255
var big: Int64 = -9_000_000_000
TypeMeaning
Int / UIntmachine word (typically 64-bit)
Int8 … Int256sized signed
UInt8 … UInt256sized unsigned
Bytealias for UInt8

Use Int for counts and indices; sized types when bit width is part of the contract. Each is an alias for a 1-lane SIMD.

Floating point

TypeMeaning
Float64IEEE double (default)
Float32IEEE single
Float16IEEE half
BFloat16brain float (ML training)
Float8_e4m3fn …8-bit (GPU, ML)
Float4_e2m1fn4-bit (Blackwell+)

No bare Float type. Each is an alias for a 1-lane SIMD.

Bounds & special values

NameMeaning
bit_width_of[Int]()64 on most platforms (from std.sys.info)
UInt8.MAX255
Int8.MIN-128
Float32.MAX_FINITElargest finite
Float32.MAXmay be inf

IEEE floats carry inf, -inf, nan, -0.0.

Conversions are explicit

var i = 42
var f = Float64(i) # Int -> Float64
var s = Int8(i) # Int -> Int8
var back = Int(Int64(i)) # round trip

# between SIMD-based types: .cast[]
var g = f.cast[DType.int32]()

Variables never convert implicitly; the compiler enforces it. Literals convert only when it can prove the result is exact.

Number literals

LiteralMeaning
42decimal Int
0xFF 0o52 0b1010hex, octal, binary
1_000_000underscores group digits
3.14 .5 2. 2.5e-3floats
2 ** 200comptime IntLiteral, comptime arbitrary precision

Leading zeros on base-10 integers are rejected. At runtime literals materialize to Int / Float64.

Collection literals

[1, 2, 3] # Array, length in the type
{"id": 1, "qty": 9} # Dict
(1, "a", 2.0) # Tuple, mixed types

Unannotated, a bracket literal defaults to Array. It adapts to the type you ask for: var x: List[Int] = [1, 2, 3].

String literals

"double" 'single'

# triple quotes: newlines and indentation included
"""line one
line two"""

r"C:\raw\path" # raw: no escape processing

"\u20AC" # lowercase \u, 4 digits: € (EURO)
"\U0001F44B" # uppercase \U, 8 digits: 👋 (above U+FFFF)

# adjacent literals join, same line or across lines:
"Hello" " world!" # -> "Hello world!"
"Content of line 1. "
"Content of line 2."

Escapes:

EscapeMeaning
\n \tnewline, tab
\" \\quote, backslash
\xHHbyte (2 hex digits)
\uHHHHUnicode (4 hex digits)
\UHHHHHHHHUnicode (8 hex digits)

Source is UTF-8. \u and \U reject surrogate code points (U+D800 to U+DFFF); code points above U+FFFF need \U, not a surrogate pair.

T-strings

var who = "Mojo"

t"Hi, {who}!" # interpolation

t"sum = {1 + 2}" # any expression

t"{{literal braces}}" # -> {literal braces}

rt"raw\path {who}" # raw t-string: \ literal, still interpolates

String(t"x = {who}") # cast to String

Interpolations evaluate at runtime.

Other things

NameMeaning
True Falseboolean values
Nonethe only NoneType value
Selfthe enclosing type
_discard a value in assignment
...marks a required trait method

Sharp edges

  • Int width is platform-dependent — use Int64 for a fixed width.
  • Integer overflow wraps — Int8(127) + 1 is -128.
  • Float-to-int truncates toward zero — Int(Float64(3.9)) is 3.
  • Float8 needs a GPU — no runtime CPU arithmetic.
  • Int128 / Int256 are software-emulated.

Coming from Python

  • No implicit numeric conversion: Int + Float64 is an error. Cast with Float64(n), Int(x).
  • Numbers are fixed-width SIMD scalars, not arbitrary-precision int; only a comptime IntLiteral is unbounded.
  • No bare float or int — pick Int, Float64, or a sized type.

Coming from C++ / Rust

  • Every scalar is a 1-lane SIMD; vectorizing widens the lane count, not a new type.
  • Integer overflow wraps (defined), not C++ undefined behavior or a Rust debug panic.
  • DType is a value-level tag that parameterizes SIMD, not a type alias.
  • Int is C++ ssize_t / Rust isize, not C++ int, which is usually 32-bit.