Using the Motoko base library

For various practical language engineering reasons, the design of Motoko strives to minimize built-in types and operations.

Instead, whenever possible, the Motoko base library provides the types and operations that make the language feel complete. However, this base library is still currently under development, and is still incomplete.

This section describes a subset of modules from the Motoko base library, with an emphasis on the modules that support core features and the libraries that are referenced in the examples. The libraries documented in this section are the most stable and least likely to change. However, all of the base library APIs are subject to change over time to varying degrees. You should note, in particular, that the size and number of modules and functions included in the base library may increase dramatically as Motoko evolves.

Importing from the base library

To import from the base library, use the import keyword. Give a local module name to introduce, in this example Debug for “Debugging”, and a URL where the import declaration may locate the imported module:

import Debug "mo:base/Debug";
Debug.print("hello world");

This example illustrates how to import Motoko code—indicated by using the mo: prefix—using the base/ base library path and the module name prelude. You can also import Motoko code and other modules using relative paths. For example, if you have created a Motoko program named in the same folder as your main program, you could include it with an import declaration like this:

import Types "./types";

Modules for data types and data structures

The base library provides one module for most basic types such as integers (Int) and characters (Char) to perform common operations. There are also modules for more advanced data structures such as Trie data structures and utility modules for performing common tasks such as iteration (Iter and debugging (Debug).