Mojo Language Reference
Free reference guide: Mojo Language Reference
About Mojo Language Reference
This Mojo Language Reference is a searchable guide to the Mojo programming language, covering basic syntax such as fn vs def function declarations, var/let variable bindings, alias compile-time constants, struct value types, and trait interface definitions.
The reference includes Mojo-specific type system details like Int, Float64, String, SIMD[type, size] hardware-accelerated vector types, DType enumerations, and UnsafePointer memory management, along with the borrowed/inout/owned ownership model for argument passing.
It also covers system-level features such as @parameter compile-time branching, @always_inline function optimization, and the complete Python interop API including Python.import_module(), PythonObject wrapper type, and seamless NumPy/Pandas integration.
Key Features
- fn vs def comparison: strict-typed fn functions versus dynamic-typed def functions
- Variable declarations with var (mutable) and let (immutable) including type annotations
- Struct and trait definitions for value-type programming and interface contracts
- SIMD[type, size] vector type reference with DType enumeration and reduce operations
- Ownership model: borrowed (read-only), inout (mutable reference), owned (ownership transfer)
- Constructor (__init__) and destructor (__del__) lifecycle management patterns
- @parameter compile-time branching and @always_inline optimization decorators
- Python interop API: import_module(), PythonObject wrapper, and NumPy/Pandas integration
Frequently Asked Questions
What is the difference between fn and def in Mojo?
In Mojo, fn requires strict type annotations for all parameters and return values, enabling full compile-time optimization. def allows dynamic typing like Python, where types are inferred at runtime. fn is preferred for performance-critical code, while def is convenient for prototyping or calling Python-style APIs.
How does the ownership model work in Mojo?
Mojo uses three argument passing conventions: borrowed (read-only access, the default), inout (mutable reference that allows modifying the original value), and owned (transfers ownership to the function, which can then consume or destroy the value). This model prevents data races and enables memory safety without garbage collection.
What is SIMD in Mojo and how do I use it?
SIMD[DType, size] is a first-class vector type in Mojo that maps directly to hardware SIMD instructions. For example, SIMD[DType.float32, 4] creates a 4-element float32 vector. You can perform element-wise operations and use methods like reduce_add() for hardware-accelerated parallel computation.
How do I call Python libraries from Mojo?
Use the Python interop API: import Python from the python module, then call Python.import_module("numpy") to import any installed Python package. The returned PythonObject wraps Python objects and supports standard Python operations. You can use NumPy, Pandas, matplotlib, and any other Python library directly.
What is the alias keyword used for in Mojo?
alias defines compile-time constants that are resolved during compilation, not at runtime. For example, alias PI = 3.14159265 or alias MAX_SIZE = 1024. Unlike var or let, alias values must be known at compile time and are typically used for configuration constants and metaprogramming parameters.
How do structs and traits work in Mojo?
Structs in Mojo are value types (like C structs) that define data and methods. They require explicit __init__ constructors with inout self. Traits define interfaces that structs can implement, similar to Rust traits or Java interfaces. A trait declares method signatures that implementing structs must provide.
What does @parameter do in Mojo?
The @parameter decorator enables compile-time branching and evaluation. Code inside @parameter if blocks is evaluated at compilation, and only the matching branch is included in the final binary. This is useful for writing platform-specific code (e.g., GPU vs CPU paths) without runtime overhead.
What types of pointers does Mojo support?
Mojo provides UnsafePointer[T] for manual memory management. You can allocate memory with UnsafePointer[Int].alloc(n), store values with ptr.store(index, value), and load values with ptr.load(index). These are "unsafe" because the programmer is responsible for proper allocation and deallocation, similar to C pointers.