One of the critical features of Mojo is its module system, which allows you to import and reuse code, access the standard library, and create your custom modules, which can be used across multiple projects. In this tutorial, you will learn how to write and use Mojo modules.
What are Mojo Modules?
A Mojo module is a file that contains Mojo code that can be imported and used by other Mojo files. This code does not include the main()
function and is not executable independently but can be included in other Mojo programs. A module can define variables, functions, classes, interfaces, and symbols that other modules can export and import. Mojo modules are similar to Python modules, but some key differences exist.
Creating a Mojo Module
To create a Mojo Module, you need to define a module file. This file should contain all the functions and classes you want to include in your module. You can then import this module into your main program and use its functions and classes. To create a Mojo Module, follow these steps:
- Create a new file with
.mojo
extension. - Define your functions and classes in this file.
- Add a
module
statement at the top of the file, followed by the name of your module. - Save the file.
Here's an example of a simple Mojo module:
module mymodule;
export fn hello(name: str) {
println("Hello, {}!", name);
}
Using Mojo Modules
Once you've created a Mojo module, you can use it in your main program. To do this, you need to import the module using an import
statement. You can then use the functions and classes defined in the module.
Here's an example of how to use the hello()
function from the mymodule
module:
import mymodule;
fn main() {
mymodule.hello("Mojo");
}
The above code will output Hello, Mojo! to the console.
Importing a Module
There are two ways to import a Mojo module:
- Direct import: Import specific functionalities from a module.
from mymodule import MyFunction
- Whole module import: Alternatively, import the entire module.
import mymodule
Aliases in Imports
You can also use aliases to make module references easier and more readable. For example, the following code imports the mymodule
module with the alias mod:
import mymodule as mod
You can then use the mod alias to access the module's functions and classes:
mod.hello("Mojo");
Access the Mojo Standard Library
The Mojo standard library is a collection of modules that provide common functionality for AI programming, such as data structures, algorithms, math, IO, networking, concurrency, and more. The standard library is distributed with the Mojo SDK, and you can access it by importing the modules using their names.
You can also import all modules from a package using the import <package_name>
syntax. For example, the following Mojo file imports the array
module from the standard library and uses its Array type to create a vector of numbers:
import array;
fn main() {
let v: Array<f64> = array.new([1.0, 2.0, 3.0]);
println("The vector is {}", v);
}
You can find the list of all modules in the current standard library in the Mojo documentation.
Conclusion
In this tutorial, you learned about Mojo Modules and how they can be used to organize your code into reusable, modular components. You also learned how to create and use a Mojo Module in your main program. With this knowledge, you can use Mojo Modules to make your code more organized and easier to manage.