Modules (v0.2.2)

Imports, aliases, and selective import scopes.

Latest

Overview

Modules are brought into a file with import. A file can import a full module, give it an alias, or import only selected names.

Use selective imports when a file only needs a few symbols. This keeps the dependency list easier to scan.

Aliases are useful when a module path is long or repeated often.

Primary source: syntax/modules/import.md

Syntax Signatures

import std::io;
import std::collections::vector;
import "other.thrust" as dep;
import "other.thrust" only { stack, counter };

Behavior and Use

Imports should show what the file actually depends on. Avoid importing large modules when one or two names are enough.

Use a consistent import style across nearby files so readers do not have to adjust to a new pattern each time.

Import Forms

Imports control which external names are available in the current file.

Name Resolution

Qualified names make module boundaries explicit.

Example

import std::io;

fn main() s32 @public {
    io::print("module import works\n");
    return 0;
}
import std::collections::vector as vec;

fn main() s32 @public {
    var values := vec::newVector[s32]();

    vec::push[s32](ref values, 10);
    vec::push[s32](ref values, 20);

    var total: s32 = vec::front[s32](ref values) + vec::back[s32](ref values);

    vec::dropVector[s32](ref values);
    return total;
}
import std::io only { print };

fn main() s32 @public {
    print("selective import keeps this file small\n");
    return 0;
}

Back to language reference