mod and use
mod foo tells the compiler: "compile and link the file foo.zolo in the same
directory". The module becomes accessible via the foo:: prefix, but the names
do not yet enter local scope — that is the job of use.
The most direct form brings in a single name — useful when you only need one function from the module:
use lib_demo::greet imports only greet; the rest of the module does not
pollute the scope.
// Feature: import a single name from the module
// Syntax: `use foo::name` — `name` becomes available without prefix
// When to use: you only need one function/constant from that module;
// avoids polluting the scope with everything.
mod lib_demo
// We only need `greet` here — we ignore the rest.
use lib_demo::greet
fn main() {
greet("zolo")
// expected: Hello, zolo!
greet("world")
// expected: Hello, world!
}
To bring in several names at once, use the brace list form:
use foo::{a, b, c} is more readable than three separate use statements when
the names come from the same module.
// Feature: import several names as a list
// Syntax: `use foo::{a, b, c}` — all become accessible without prefix
// When to use: you need multiple things from the same module, but
// not all of them; more readable than multiple `use foo::name`.
mod lib_demo
// Imports three names at once.
use lib_demo::{add, sub, greet}
fn main() {
greet("operations")
// expected: Hello, operations!
print("10 + 3 = {add(10, 3)}")
// expected: 10 + 3 = 13
print("10 - 3 = {sub(10, 3)}")
// expected: 10 - 3 = 7
}
The following example combines mod and the list import to access functions and
a public constant:
mod lib_demo loads the file; use lib_demo::{greet, add, get_pi, VERSION}
makes all four names available without a prefix.
// Feature: declare a module with `mod` and use it via qualified path
// Syntax: `mod foo` — registers `foo.zolo` as module `foo`
// `use foo` + `foo::name` — call with prefix
// When to use: most explicit form; good when you want to make
// clear which module each name comes from.
// Loads the lib_demo.zolo file as a module.
mod lib_demo
// SKIP: `use lib_demo` (whole-module qualified access) currently
// fails to load — the lowering treats it as `use lib_demo::lib_demo`
// and complains about a missing `''` module. Use destructured names
// (`use lib_demo::greet`) or a list import instead, and call them
// directly without the prefix.
use lib_demo::{greet, add, get_pi, VERSION}
fn main() {
greet("zolo")
// expected: Hello, zolo!
let s = add(2, 3)
print("sum = {s}")
// expected: sum = 5
print("pi = {get_pi()}")
// expected: pi = 3.14159
print("version = {VERSION}")
// expected: version = 1.0.0
}
Challenge
Add sub to the import list of the last example and print sub(10, 3).
What happens if you try to use a name that is not in the list?
See also