Skip to main content

Mojo reference

This section includes the Mojo API references:

How to read the Mojo API docs

Mojo syntax is covered in detail in the Mojo manual. Here's a quick cheat-sheet on reading struct and function signatures.

Arguments

Function arguments appear in parentheses after the function name:

fn example_fn(pos: Int, /, pos_or_kw: Int, *, kw_only: Bool = False):
    ...

Here's a quick overview of some special syntax in the argument list:

You may also see argument names prefixed with one or two stars (*):

def myfunc2(*names, **attributes):

An argument may also be preceded by an argument convention, which indicates how the value is passed:

fn sort(mut names: List[String]):

The most common conventions are:

  • read(default): the callee receives an immutable reference to the value.
  • mut: the callee receives a mutable reference to the value.
  • owned: the callee receives ownership of a value.

For details and a complete list of argument conventions, see Argument conventions.

Parameters

Mojo structs and functions can take parameters. Parameters are evaluated at compilation time, and act as constants at runtime. Parameter lists are enclosed in square brackets:

struct ExampleStruct[size: Int, //, thing: Thing[size]]:

Parameters that occur before a double-slash (//) in the parameter list are infer-only parameters. You usually don't need to specify infer-only parameters; as the name suggests, they're usually inferred.

Like arguments, parameters can be positional-only, keyword-or-positional, or keyword-only, and they can be required or optional. The /, *, and = characters have the same meaning in parameter lists as they do in argument lists.

Standard library

The Mojo standard library provides nearly everything you'll need for writing Mojo programs, including basic data types like Int and SIMD, collection types like List, reusable algorithms and modules to support GPU programming.

Top-level packages:

algorithm

High performance data operations: vectorization, parallelization, reduction, memory.

base64

Binary data encoding: base64 and base16 encode/decode functions.

benchmark

Performance benchmarking: statistical analysis and detailed reports.

bit

Bitwise operations: manipulation, counting, rotation, and power-of-two utilities.

builtin

Language foundation: built-in types, traits, and fundamental operations.

collections

Core data structures: List, Dict, Set, Optional, plus specialized collections.

compile

Runtime function compilation and introspection: assembly, IR, linkage, metadata.

complex

Complex numbers: SIMD types, scalar types, and operations.

documentation

Documentation built-ins: decorators and utilities for doc generation.

gpu

GPU programming primitives: thread blocks, async memory, barriers, and sync.

hashlib

Cryptographic and non-cryptographic hashing with customizable algorithms.

io

Core I/O operations: console input/output, file handling, writing traits.

iter

Iteration traits and utilities: Iterable, Iterator, enumerate, zip, map.

itertools

Iterator tools: count, product, repeat for lazy sequence generation.

logger

Logging with configurable severity levels.

math

Math functions and constants: trig, exponential, logarithmic, and special functions.

memory

Low-level memory management: pointers, allocations, address spaces.

os

OS interface layer: environment, filesystem, process control.

pathlib

Filesystem path manipulation and navigation.

prelude

Standard library prelude: fundamental types, traits, and operations auto-imported.

pwd

Password DB Lookups. User account information.

python

Python interoperability: import modules, call functions, type conversion.

random

Pseudorandom number generation with uniform and normal distributions.

runtime

Runtime services: async execution and program tracing.

stat

File type constants and detection from stat system calls.

subprocess

Execute external processes and commands.

sys

System runtime: I/O, hardware info, FFI, intrinsics, compile-time utils.

tempfile

Manage temporary files and directories: create, locate, and cleanup.

testing

Unit testing: Assertions (equal, true, raises) and test suites.

time

Timing operations: monotonic clocks, performance counters, sleep, time_function.

utils

General utils: indexing, variants, static tuples, and thread synchronization.

MAX AI kernels library

The MAX AI kernels library provides a collection of highly optimized, reusable compute kernels for high-performance numerical and AI workloads. These kernels serve as the foundational building blocks for writing MAX custom operations or standalone GPU kernels that are portable across CPUs and GPUs.

Top-level packages:

Decorators

A Mojo decorator is a higher-order function that modifies or extends the behavior of a struct, a function, or some other code.

Was this page helpful?