Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Modules

Creating a file implicitly creates a module whose the namespace is the file's path.

You can import values and types of different modules with import statements, that must appear first in the module.

import MyModule
// Now you can use the MyModule.some_value syntax to access to
// `some_value` defined in MyModule
// You can also access types and constructors

Imports can be unqualified too:

import MyModule.{some_value, MyType(..)}
// this adds to the scope:
// the `some_value` value
// the `MyType` type
// `MyType` constructors (because of the `(..)`)

You can import nested modules using the import My/Nested/Module syntax

Visibility

Both values and types are module-private by default. You can use the pub modifier to make them visible from other modules:

pub let my_value = // ..

pub type MyAlias = // ..

pub enum MyEnum {
  // ..
}

pub struct MyStruct {
  // ..
}

In the case of enums and structs, just using pub will not expose the underlying implementation (the fields or the costructors). You can expose that using the pub(..) visibility:

pub(..) enum MyEnum {
  // ..
}
pub(..) struct MyStruct {
  // ..
}