pub and Visibility
Control access.
pub and Visibility is a free Learn Rust Coding lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Private by Default
In Rust, everything inside a module is private by default. Other modules cannot see it unless you opt in with pub.
This safe default keeps implementation details hidden until you deliberately expose them.
pub Functions
Marking a function pub makes it callable from outside its module. Without it, the call fails to compile.
mod math {
pub fn square(x: i32) -> i32 {
x * x
}
}
fn main() {
println!("{}", math::square(5));
}Private Helpers Stay Hidden
You can keep internal helpers private while exposing a public API. The public function may call the private one, but outsiders cannot.
mod math {
fn double(x: i32) -> i32 { x * 2 }
pub fn quadruple(x: i32) -> i32 { double(double(x)) }
}
fn main() {
println!("{}", math::quadruple(3));
}pub Structs Hide Fields
Making a struct pub exposes the type, but its fields stay private unless each is also marked pub.
This lets you control how the struct is created and modified.
mod model {
pub struct User {
pub name: String,
age: u32,
}
impl User {
pub fn new(name: String, age: u32) -> User {
User { name, age }
}
pub fn age(&self) -> u32 { self.age }
}
}
fn main() {
let u = model::User::new(String::from("Sam"), 28);
println!("{} is {}", u.name, u.age());
}Why Hide Fields?
Private fields enforce invariants. Callers must use your constructor and methods, so you can validate inputs and change the internal layout later without breaking users.
pub Enums Expose Everything
Unlike structs, a pub enum makes all its variants public automatically. Variants have no separate visibility.
mod traffic {
pub enum Light {
Red,
Yellow,
Green,
}
}
fn main() {
let l = traffic::Light::Green;
let go = matches!(l, traffic::Light::Green);
println!("go = {}", go);
}pub(crate): Crate-Wide
pub(crate) makes an item visible everywhere in the same crate, but not to outside crates that depend on yours.
It is perfect for internal APIs shared across modules.
mod config {
pub(crate) fn default_port() -> u16 {
8080
}
}
fn main() {
println!("{}", config::default_port());
}pub(super): Parent-Only
pub(super) exposes an item only to the parent module. It is a narrower grant than full pub.
mod outer {
pub fn run() {
inner::helper();
}
mod inner {
pub(super) fn helper() {
println!("visible to parent only");
}
}
}
fn main() {
outer::run();
}Re-exporting With pub use
pub use re-exports an item, letting callers reach it through a shorter or more convenient path. This shapes a clean public API.
mod deep {
pub mod nested {
pub fn ping() {
println!("pong");
}
}
pub use nested::ping;
}
fn main() {
deep::ping();
}Designing a Public API
Expose the smallest surface that gets the job done. Keep helpers private, hide struct fields behind methods, and use pub(crate) for internal sharing.
A tight API is easier to evolve without breaking users.
Visibility Levels Summary
From most to least restrictive:
- default (private to the module)
pub(super)— visible to the parentpub(crate)— visible across the cratepub— visible everywhere
Quick Check
Test your grasp of visibility.
Recap
You learned to control access:
- Items are private by default;
pubexposes them pubstructs keep fields private unless each ispubpubenums expose all variantspub(crate)andpub(super)give finer controlpub usere-exports to shape a clean API
Frequently asked questions
Is the “pub and Visibility” lesson free?
Yes — the full text of “pub and Visibility” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “pub and Visibility”?
Control access. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “pub and Visibility” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Modules and mod
- pub and Visibility
- use and Paths
- Crates and the Crate Root