APIs and reference
This section includes the API reference for the Mojo standard library, as well as a reference for Mojo decorators and the Mojo CLI.
Mojo 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.
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):
...
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:
-
Slash (
/
): arguments declared before a slash are positional-only arguments. -
Star (
*
): a star by itself in place of an argument indicates that the arguments after the star are keyword-only. -
An equals sign (
=
) introduces a default value for an optional argument.
You may also see argument names prefixed with one or two stars (*
):
def myfunc2(*names, **attributes):
def myfunc2(*names, **attributes):
-
An argument name prefixed by a single star character, like
*names
identifies a variadic argument. -
An argument name prefixed with a double star, like
**attributes
identifies a variadic keyword-only argument.
An argument may also be preceded by an argument convention, which indicates how the value is passed:
fn sort(mut names: List[String]):
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]]:
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.
Top-level packages
algorithm
base64
benchmark
bit
buffer
builtin
collections
compile
complex
gpu
hashlib
layout
logger
math
memory
os
pathlib
pwd
python
random
stat
sys
tempfile
testing
time
utils
Mojo decorators
A Mojo decorator is a higher-order function that modifies or extends the behavior of a struct, a function, or some other code. You'll find a list of built-in Mojo decorators here.
@__copy_capture
@always_inline
@implicit
@nonmaterializable
@parameter
@register_passable
@staticmethod
@value
Mojo CLI reference
The Mojo CLI provides commands to help you develop, run, build, and debug Mojo programs.
mojo
mojo build
mojo debug
mojo demangle
mojo doc
mojo format
mojo package
mojo repl
mojo run
mojo test
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!