Visibility and pub
On this page
In Zolo, everything not marked with pub is private to the file where it
was declared. Another module that imports the file only sees the public items —
functions, constants, structs, enums, traits, and newtypes all follow the same
rule.
The support module lib_demo.zolo illustrates this well: greet, add, sub,
get_pi, and VERSION are public; internal_helper has no pub and remains
invisible from outside:
Only items marked pub appear in the use list. Trying to import
internal_helper would cause a compile error.
// Feature: visibility — `pub` exports, no `pub` is private
// Syntax: `pub fn`, `pub const`, `pub struct`, etc. make the
// item visible to other modules. Without `pub`, it is restricted to
// the file.
// When to use: encapsulate internal details, expose only the API.
mod lib_demo
// Only the public API is importable.
use lib_demo::{greet, add, get_pi, VERSION}
fn main() {
greet("public api")
print("add(1,2) = {add(1, 2)}")
print("pi = {get_pi()}")
print("version = {VERSION}")
// expected:
// Hello, public api!
// add(1,2) = 3
// pi = 3.14159
// version = 1.0.0
// Note: `internal_helper` is in lib_demo.zolo WITHOUT `pub`,
// so it cannot be imported or called from here.
// Trying `use lib_demo::internal_helper` or
// `lib_demo::internal_helper()` is a design error — the
// function belongs only to the module.
}
The rule is simple: if you want other files to use an item, add pub. If it is
an implementation detail, leave it out — the compiler will reject any attempt at
external access.
pub const VERSIONexports the constant.const VERSION(withoutpub) would be hidden — even though it is inlib_demo.zolo, no other file could reference it.
See also