Rust Modules Quiz
40 comprehensive questions on Rust's module system, covering module creation, visibility, imports, paths, and the use keyword — with 11 code examples demonstrating practical module organization patterns.
Question 1
What is a module in Rust?
Question 2
How do you define a module in Rust?
Question 3
What is the default visibility of items in a module?
Question 4
How do you make an item public in a module?
Question 5
What will this module code do?
mod my_module {
pub fn public_function() {
println!("This is public");
}
fn private_function() {
println!("This is private");
}
}
fn main() {
my_module::public_function();
my_module::private_function();
}Question 6
How do you access items from a module?
Question 7
What is the use keyword used for?
Question 8
How do you use the use keyword to import items?
Question 9
What will this use statement do?
mod network {
pub mod client {
pub fn connect() {}
}
}
use network::client;
fn main() {
client::connect();
}Question 10
How do you import multiple items with a single use statement?
Question 11
What is an absolute path in Rust modules?
Question 12
What is a relative path in Rust modules?
Question 13
What does super:: refer to in a module path?
Question 14
What does self:: refer to in a module path?
Question 15
How do you organize modules across multiple files?
Question 16
What is the purpose of mod.rs in a module directory?
Question 17
How do you declare a submodule in mod.rs?
Question 18
What is re-exporting in modules?
Question 19
What will this re-export code do?
mod network {
pub mod client {
pub fn connect() {}
}
}
pub use network::client::connect;
fn main() {
connect(); // Can now call directly
}Question 20
What is the crate root in Rust?
Question 21
In a scenario where you're building a large application with networking, database, and UI components, how should you organize the modules?
Question 22
What is the difference between pub and pub(crate)?
Question 23
How do you make a struct's fields public?
Question 24
What happens if you try to access a private field of a public struct?
Question 25
How do you organize tests in modules?
Question 26
What is the purpose of the #[cfg(test)] attribute?
Question 27
How do you access the crate root from anywhere in the crate?
Question 28
What is the difference between use and mod declarations?
Question 29
How do you handle name conflicts when importing items?
Question 30
What will this code with name aliasing do?
mod math {
pub fn add(x: i32, y: i32) -> i32 { x + y }
}
use math::add as sum;
fn main() {
println!("Result: {}", sum(3, 4));
}Question 31
How do you import all public items from a module?
Question 32
When should you avoid using glob imports (use module::*)?
Question 33
How do you structure a large project with many modules?
Question 34
What is the main.rs file in a binary crate?
Question 35
What is the lib.rs file in a library crate?
Question 36
How do you make a module accessible from tests?
Question 37
What is the purpose of module documentation comments?
Question 38
How do you handle cyclic dependencies between modules?
Question 39
What is the benefit of using modules for code organization?
Question 40
In a complex web application with authentication, routing, database access, and API endpoints, how would you structure the module hierarchy?
