Modules (v0.2.1)
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.
Best Practices
- Use `only` imports when a file needs just a few names.
- Use aliases when module paths become repetitive.
- Avoid circular imports by moving shared definitions into smaller files.
Import Forms
Imports control which external names are available in the current file.
- import std::io brings a module into scope through its module path.
- import "file.thrust" imports a source file by path.
- as gives an imported module or file a shorter local name.
- only limits the import to selected symbols. Use it when a file depends on a small part of another module.
Name Resolution
Qualified names make module boundaries explicit.
- Use module::symbol when a name comes from an imported module.
- Aliases reduce repeated long paths but should still describe the imported code.
- If two imports expose the same unqualified name, keep the access qualified instead of relying on reader memory.
Example
import std::io;
fn main() s32 @public {
io::print("module import works\n");
return 0;
}