📖 Scenario: You are building a small Rust project that organizes code into modules. You want to control which parts of your code are visible outside their modules, just like how you might decide which rooms in a house are open to guests.
🎯 Goal: Learn how to use pub to make modules and functions visible outside their module, and how to keep some parts private.
📋 What You'll Learn
Create a module named kitchen with a private function
Create a module named living_room with a public function
Use pub keyword to control visibility
Call the public function from living_room in main
Try to call the private function from kitchen in main and observe the error
💡 Why This Matters
🌍 Real World
In real Rust projects, controlling module visibility helps keep code clean and prevents accidental use of internal details.
💼 Career
Understanding module visibility is essential for writing maintainable Rust code in professional software development.
Progress0 / 4 steps
1
Create the kitchen module with a private function
Create a module called kitchen with a function named cook_food that prints "Cooking food...". Do NOT use pub before the function, so it stays private.
Rust
Hint
Remember, functions are private by default in Rust modules unless you add pub.
2
Create the living_room module with a public function
Add a module called living_room with a function named watch_tv that prints "Watching TV...". Make this function public by adding pub before it.
Rust
Hint
Use pub fn to make the function visible outside the module.
3
Call the public function from living_room in main
Write a main function that calls the public function watch_tv from the living_room module.
Rust
Hint
Use the full path living_room::watch_tv() to call the public function.
4
Try to call the private function from kitchen in main and observe the error
In the main function, add a call to kitchen::cook_food(). Run the program and observe the error message about visibility.
Rust
Hint
This will cause a compile error because cook_food is private. This shows how Rust controls module visibility.
Practice
(1/5)
1. What does the pub keyword do in Rust module visibility?
easy
A. Makes a function or item accessible outside its module
B. Makes a function private to its module
C. Deletes the function from the module
D. Renames the function for external use
Solution
Step 1: Understand the role of pub
The pub keyword in Rust is used to make functions, structs, or other items accessible from outside the module they are defined in.
Step 2: Contrast with default visibility
By default, items are private and cannot be accessed outside their module unless marked with pub.
Final Answer:
Makes a function or item accessible outside its module -> Option A
Quick Check:
pub = public access [OK]
Hint: Remember: pub means public, so others can use it [OK]
Common Mistakes:
Thinking pub hides the item
Confusing pub with renaming
Assuming pub deletes the item
2. Which of the following is the correct way to make a struct public in Rust?
easy
A. struct MyStruct { pub field: i32 }
B. struct pub MyStruct { field: i32 }
C. pub struct MyStruct { field: i32 }
D. struct MyStruct pub { field: i32 }
Solution
Step 1: Recall correct syntax for public structs
In Rust, to make a struct public, the pub keyword must come before the struct keyword.
Step 2: Check placements of pub
pub struct MyStruct { field: i32 } correctly places pub before struct. Placing pub inside the braces (on field), writing struct pub MyStruct, or struct MyStruct pub {} are incorrect.
Final Answer:
pub struct MyStruct { field: i32 } -> Option C
Quick Check:
pub struct is correct syntax [OK]
Hint: pub goes before struct, not inside or after [OK]
A. secret function is private and cannot be called from main
B. Missing semicolon after println!
C. Module my_mod is not declared public
D. Function secret must have pub keyword inside main
Solution
Step 1: Check function visibility
The function secret is not marked pub, so it is private to my_mod.
Step 2: Understand call from main
Since main is outside my_mod, it cannot call a private function, causing a compile error.
Final Answer:
secret function is private and cannot be called from main -> Option A
Quick Check:
Private functions can't be accessed outside module [OK]
Hint: Private functions can't be called outside their module [OK]
Common Mistakes:
Thinking module must be pub to call function
Assuming missing semicolon causes error
Believing pub inside main is needed
5. You have a private struct Config inside a module, but want to allow other modules to create instances with limited access to its fields. Which visibility setup is correct?
hard
A. Make struct private and fields public
B. Make struct pub but keep fields private
C. Make both struct and fields private
D. Keep struct private and make fields pub
Solution
Step 1: Understand struct and field visibility
Making the struct pub allows other modules to create instances, but keeping fields private restricts direct access to internal data.
Step 2: Evaluate other approaches
Keeping the struct private (with or without pub fields) prevents other modules from constructing instances. Making everything private blocks access entirely.
Final Answer:
Make struct pub but keep fields private -> Option B