A module in Rust is a way to organize code into separate namespaces. It helps group related functions, structs, and other items together, making code easier to manage and understand.
Click to reveal answer
beginner
How do you define a module inside a Rust file?
You define a module using the mod keyword followed by the module name and curly braces containing the module's code. For example:
mod greetings { fn hello() { println!("Hi!"); } }
Click to reveal answer
intermediate
What is the difference between mod and use in Rust modules?
mod defines a new module or declares it exists, while use brings items from a module into scope so you can use them without full paths.
Click to reveal answer
beginner
How do you make a function inside a module accessible from outside the module?
You add the pub keyword before the function to make it public. For example:
pub fn greet() { println!("Hello!"); }
Without pub, the function is private to the module.
Click to reveal answer
intermediate
How can you split a module into multiple files in Rust?
You create a folder with the module name and add a mod.rs file inside it, or in newer Rust versions, create a file named after the module. Then, submodules are files inside that folder. This helps organize large modules.
Click to reveal answer
Which keyword is used to define a module in Rust?
Afn
Buse
Cmod
Dpub
✗ Incorrect
The mod keyword defines a module in Rust.
How do you make a function inside a module accessible from other modules?
AUse <code>use</code> before the function
BUse <code>mod</code> before the function
CFunctions are always accessible
DAdd <code>pub</code> before the function
✗ Incorrect
Functions are private by default. Adding pub makes them accessible outside the module.
What does the use keyword do in Rust modules?
ADefines a new module
BImports items from a module into scope
CMakes a function public
DDeletes a module
✗ Incorrect
use brings items from a module into the current scope for easier access.
If you define mod greetings {} inside main.rs, where can you use the items inside greetings?
AAnywhere in the crate if items are public
BOnly inside <code>greetings</code>
COnly outside <code>greetings</code>
DNowhere
✗ Incorrect
Public items inside a module can be used anywhere in the crate.
How can you organize a large module into multiple files?
ABy creating submodules in separate files
BBy putting all code in one file
CBy using <code>use</code> to import files
DRust does not support multiple files for modules
✗ Incorrect
Large modules can be split into submodules placed in separate files or folders.
Explain how to define a module in Rust and make a function inside it accessible from outside.
Think about keywords to create modules and control access.
You got /3 concepts.
Describe how Rust modules help organize code and how you can split modules into multiple files.
Consider how big projects keep code tidy.
You got /4 concepts.
Practice
(1/5)
1. What keyword do you use in Rust to define a new module inside a file?
easy
A. module
B. use
C. mod
D. pub
Solution
Step 1: Understand Rust module declaration
Rust uses the keyword mod to declare a module inside a file.
Step 2: Differentiate from other keywords
use imports modules, pub makes items public, and module is not a Rust keyword.
Final Answer:
mod -> Option C
Quick Check:
Keyword for defining modules = mod [OK]
Hint: Remember: 'mod' means module in Rust [OK]
Common Mistakes:
Using 'module' instead of 'mod'
Confusing 'use' with 'mod'
Forgetting 'mod' keyword
2. Which of the following is the correct syntax to declare a module named network in Rust?
easy
A. use network {}
B. module network {}
C. pub mod network() {}
D. mod network {}
Solution
Step 1: Recall correct module syntax
The correct syntax to declare a module is mod name { } without parentheses.
Step 2: Check each option
mod network {} uses mod network {} which is correct. module network {} uses 'module' which is invalid. pub mod network() {} adds parentheses which are not allowed. use network {} uses 'use' which is for importing.
Final Answer:
mod network {} -> Option D
Quick Check:
Module declaration = mod name {} [OK]
Hint: Modules use curly braces, no parentheses [OK]
Common Mistakes:
Adding parentheses after module name
Using 'module' instead of 'mod'
Confusing 'use' with 'mod'
3. What will be the output of this Rust code?
mod math {
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
}
fn main() {
println!("{}", math::add(3, 4));
}
medium
A. 7
B. Compilation error
C. math::add(3, 4)
D. 0
Solution
Step 1: Check module and function visibility
The function add is marked pub, so it can be accessed outside the module.
Step 2: Evaluate the function call
Calling math::add(3, 4) returns 3 + 4 = 7, which is printed.
Final Answer:
7 -> Option A
Quick Check:
Public function call returns sum = 7 [OK]
Hint: Public functions in modules can be called with module::function [OK]
A. Function 'helper' is private and cannot be accessed outside the module
B. Missing semicolon after println!
C. Module 'utils' is not declared correctly
D. Cannot call functions inside modules
Solution
Step 1: Check function visibility
The function helper is not marked pub, so it is private to the module.
Step 2: Understand access from main
Trying to call utils::helper() from outside causes a compilation error due to privacy.
Final Answer:
Function 'helper' is private and cannot be accessed outside the module -> Option A
Quick Check:
Private functions can't be called outside module [OK]
Hint: Add 'pub' to functions to access them outside modules [OK]
Common Mistakes:
Assuming all functions are public by default
Ignoring privacy rules
Thinking modules block all calls
5. You want to organize your Rust project by splitting code into two files: main.rs and network.rs. How do you correctly declare and use the network module in main.rs?
hard
A. Write pub mod network {} in main.rs and put code inside network.rs
B. Write mod network; in main.rs and make functions in network.rs public with pub
C. Write use network; in main.rs and declare mod network {} inside network.rs
D. Write extern crate network; in main.rs and import functions manually
Solution
Step 1: Understand module file linking
To link an external file as a module, use mod network; in main.rs. This tells Rust to load network.rs.
Step 2: Make functions public in network.rs
Functions inside network.rs must be marked pub to be accessible from main.rs.
Final Answer:
Write mod network; in main.rs and make functions in network.rs public with pub -> Option B
Quick Check:
Use mod filename; and pub functions for multi-file modules [OK]
Hint: Use 'mod filename;' in main and 'pub' for access [OK]
Common Mistakes:
Using 'use' instead of 'mod' to declare modules
Declaring mod inside the module file
Using extern crate for local modules
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.