A module in Mojo is analogous to a module in languages like Python; it's a single file containing code (functions, structs, etc.) that can be imported and reused in other Mojo files. Key points include:

Example Breakdown: mymodule.mojo

mojoCopy code
struct MyPair:
    var first: Int
    var second: Int

    fn __init__(inout self, first: Int, second: Int):
        self.first = first
        self.second = second

    fn dump(self):
        print(self.first, self.second)

Importing Modules

You can import modules using the import statement or from ... import ... syntax, similar to Python. The examples show three ways to import and use the MyPair struct from mymodule.mojo:

  1. Direct import of a specific symbol: from mymodule import MyPair
  2. Importing the entire module: import mymodule
  3. Importing with an alias: import mymodule as my

Packages in Mojo

A package in Mojo is a collection of modules in a directory, which must include an __init__.mojo file to be recognized as a package. This structure allows for more organized code and can be compiled into a package file for easier distribution.

Key Points on Packages

Practical Applications