📖 Scenario: Imagine you are organizing your study notes. Instead of keeping all notes in one big pile, you separate them into folders by subject. This makes it easier to find and manage your notes.
🎯 Goal: You will create a simple Rust program that uses modules to organize functions. This will show how modules help keep code neat and easy to use.
📋 What You'll Learn
Create a module named math_utils with a function add that adds two numbers
Create a module named string_utils with a function greet that returns a greeting message
Use these modules in the main function to call add and greet
Print the results of calling these functions
💡 Why This Matters
🌍 Real World
In real software projects, modules help keep code organized and prevent confusion when many people work on the same code.
💼 Career
Understanding modules is important for writing clean, maintainable code in Rust and many other programming languages.
Progress0 / 4 steps
1
Create the math_utils module with an add function
Create a module called math_utils with a public function add that takes two i32 numbers named a and b and returns their sum.
Rust
Hint
Use mod to create a module and pub fn to make the function accessible outside the module.
2
Create the string_utils module with a greet function
Add a module called string_utils with a public function greet that takes a &str parameter named name and returns a String greeting like "Hello, {name}!".
Rust
Hint
Use format! macro to create the greeting string.
3
Use the modules in the main function
Write a main function that calls math_utils::add(5, 3) and stores the result in sum. Then call string_utils::greet("Alice") and store the result in greeting.
Rust
Hint
Use the module name followed by :: and the function name to call the function.
4
Print the results from the modules
Add println! statements in main to print "Sum: {sum}" and "Greeting: {greeting}".
Rust
Hint
Use println!("Sum: {}", sum); and println!("Greeting: {}", greeting); to show the results.
Practice
(1/5)
1. Why do Rust programmers use modules in their code?
easy
A. To organize code into smaller, manageable parts
B. To make the program run faster
C. To increase the size of the compiled file
D. To avoid writing comments
Solution
Step 1: Understand the purpose of modules
Modules help break down code into smaller parts that are easier to manage and understand.
Step 2: Compare other options
Options A, B, and D do not describe the main reason for using modules; they are unrelated to code organization.
Final Answer:
To organize code into smaller, manageable parts -> Option A
Quick Check:
Modules organize code = C [OK]
Hint: Modules help split code into parts for clarity [OK]
Common Mistakes:
Thinking modules speed up the program
Believing modules increase file size
Confusing modules with comments
2. Which of the following is the correct way to declare a module named math in Rust?
easy
A. module math { }
B. mod math { }
C. package math { }
D. use math { }
Solution
Step 1: Recall Rust module syntax
Rust uses the keyword mod to declare a module.
Step 2: Check other options
Options B, C, and D use incorrect keywords not valid in Rust for modules.
Final Answer:
mod math { } -> Option B
Quick Check:
Use mod to declare modules = A [OK]
Hint: Use 'mod' keyword to declare modules in Rust [OK]
Common Mistakes:
Using 'module' instead of 'mod'
Confusing 'use' with module declaration
Trying 'package' keyword like other languages
3. What will be the output of this Rust code?
mod greetings {
pub fn hello() {
println!("Hello from module!");
}
}
fn main() {
greetings::hello();
}
medium
A. Compilation error: module not found
B. Error: function hello is private
C. Hello from module!
D. No output
Solution
Step 1: Check function visibility
The function hello is marked pub, so it is accessible outside the module.
Step 2: Understand function call
The main function calls greetings::hello(), which prints the message.
Final Answer:
Hello from module! -> Option C
Quick Check:
Public function call prints message = D [OK]
Hint: Public functions in modules can be called outside [OK]
Common Mistakes:
Forgetting to use 'pub' makes function private
Assuming no output without checking visibility
Thinking module must be imported separately
4. Identify the error in this Rust code using modules:
A. Function hammer is private and cannot be accessed outside the module
B. Module tools is not declared correctly
C. Missing semicolon after println!
D. Cannot call functions inside modules
Solution
Step 1: Check function visibility
The function hammer is not marked pub, so it is private to the module.
Step 2: Understand access rules
Trying to call tools::hammer() from main causes an error because the function is private.
Final Answer:
Function hammer is private and cannot be accessed outside the module -> Option A
Quick Check:
Private function access error = A [OK]
Hint: Mark functions 'pub' to access outside modules [OK]
Common Mistakes:
Assuming all module functions are public by default
Thinking module declaration is wrong
Missing semicolon in println! (actually correct here)
5. You want to organize a Rust project with two modules: math and utils. The math module has a public function add that you want to use in utils. How should you structure the code to allow utils to call math::add?
hard
A. Make add private and call it directly from utils
B. Modules cannot call functions from each other
C. Define add inside utils instead of math
D. Declare both modules in the root, mark add as pub, and use use crate::math::add; inside utils
Solution
Step 1: Make function public
The add function must be marked pub to be accessible outside math.
Step 2: Use 'use' to import function
Inside utils, use use crate::math::add; to bring add into scope for calling.
Final Answer:
Declare both modules in the root, mark add as pub, and use use crate::math::add; inside utils -> Option D
Quick Check:
Public function + use import = B [OK]
Hint: Make functions pub and import with use to share between modules [OK]
Common Mistakes:
Leaving function private and expecting access
Trying to call private functions directly
Thinking modules cannot share code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.