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: Nightly
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 conversions cheat sheet

Make one, convert one, access one. Remember to cast.
Download PDF
v1.1.0dev

Numbers

Intmachine-width integer; a 1-lane SIMD

make
42 # IntLiteral
Int(x) # any Intable: Bool, Int8 …
Int(s) # any Intable that's raising: String
Int(Int32(5)) # any numeric scalar, cross-dtype
convert
Float64(i), UInt(i) # to float, to unsigned (same bits)
String(i), Bool(i) # to text, True if non-zero
i.cast[DType.int8]() # to another dtype

Float to Int truncates toward zero; cross-dtype casts wrap (two's complement). Int is Scalar[DType.int], so .cast works like any SIMD scalar.

Int and UInt share bits: Int(-1) is UInt 2^64-1.

Float64IEEE double; a 1-lane SIMD

make
3.14 # FloatLiteral
Float64(x) # any Floatable: Int, Bool
Float64(s) # any Floatable that's raising: String
Float32(x).cast[DType.float64]() # widen a scalar
convert
Int(f) # truncates toward zero
f.cast[DType.float32]() # narrow (precision loss)
Bool(f), String(f) # True if non-zero, to text

No bare Float, and no Float128/256. Convert all other floats (including special purpose) with .cast().

Booltrue / false, and what's truthy

make
Bool(x) # any Boolable
convert
Int(b), Float64(b), String(b) # 0 or 1, 0.0 or 1.0, "True" or "False"
truthy — for if / while / and / or / Bool()
KindTruthy when
Numbersnon-zero
Strings & collectionsnon-empty
OptionalNone False, else True
PythonObjectPython's own rules

Any type with a __bool__ is truthy. Converting a Bool to a number is explicit: Int(b), never implicit.

SIMD[T, N]N lanes of one dtype; Scalar = SIMD[T, 1]

make
SIMD[T, N](x) # splat one value to all lanes
SIMD[T, 4](a, b, c, d) # per-lane
convert
v.cast[DType.x]() # new dtype, same lane count
SIMD[T, N](scalar) # splat a Scalar up to N lanes

N is the lane count, not bit width; a lane's bit width is its DType. Int, Float64, Int8 … are all SIMD scalars.

Text

Stringowned, growable UTF-8 text

make
String(x) # any StringSpan, StringLiteral, Writable (Int, Float64, Bool …)
String(t"{x}") # Not needed for print()
String(from_utf8=bytes) # raises on bad UTF-8
String(from_utf8_lossy=bytes) # replaces bad bytes
convert
Int(s) # base-10 parse; raises on "3.5", "0xff", ""
Float64(s) # parse (1e3, inf ok); raises "", garbage
Bool(s) # True if non-empty
access (by byte / codepoint / grapheme)
s[byte=i], s[byte=i:j] # also for codepoint and single index grapheme
s.as_bytes(), s.codepoints(), s.graphemes() # iterators

Pointers

Pointer[T]to backing buffer

Use unsafe_ptr() to access: List, String, StringSpan, Array, and Span.

access through a pointer
buf.unsafe_ptr() # -> Pointer[T]
p[unsafe_offset=i] # deref one element
p.unsafe_offset(i)[] # pointer arithmetic, then deref
p.unsafe_load[width=N]() # read N lanes -> SIMD[T, N]
vectorize a buffer (the escape hatch)
var v = data.unsafe_ptr().unsafe_load[width=8]() # 8 elements -> one SIMD
var total = v.reduce_add() # SIMD-wide reduce

Pointer is non-null by design. Use OptionalPointer for a nullable pointer. Unsafe operations carry the unsafe_ prefix or an unsafe_ keyword.

Collections

List[T]growable, homogeneous sequence

make
var x: List[T] = [a, b, c] # annotate: unannotated defaults to Array
List[T](capacity=n) # empty; initial room for n
List[T](length=n, fill=x) # n copies of x (T: Copyable)
List(range(n)) # materialize a range
List(iterable) # from any iterator / iterable
access
list[i], list[i:j] # element by ref, Span view (no copy)
list.unsafe_take_allocation() # hand off the buffer as an Allocation[T]

Dict[K, V]hash map; keys Hashable + Equatable + Movable

make
Dict[K, V]() # empty; fill with d[k] = v
Dict[K, V](capacity=n) # empty; initial room for n
Dict.fromkeys(keys, v) # every key maps to v
access
d.setdefault(key, default) # ref; inserts default if absent
d.get(key) d.find(key) # Optional[V]
d.keys() d.values() # lazy iterators
d.items() # iterator of DictEntry (.key / .value)
d.pop(key) # value, removes it

Optional[T]a value, or nothing

make
Optional(x) # from a value (T inferred from x)
Optional[T](), Optional[T](None) # empty
access
o.value(), o.take(), o[] # ref, move out, ref (abort, abort, raise)
o.or_else(default) # value, or default