Rust Interview Questions
Rust
Web DevelopmentIoTQuestion 18
How do you create and use modules in Rust?
Answer:
In Rust, modules are a way to organize code within a crate, allowing you to split your code into smaller, more manageable pieces. Modules help in managing the scope and privacy of functions, types, and constants. Here's how to create and use modules in Rust:
Creating Modules
Modules are created using the mod
keyword. There are two main ways to define modules: inline and in separate files.
Inline Modules
You can define modules inline within a file using the mod
keyword and a block of code.
// src/main.rs or src/lib.rs
mod math {
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn subtract(a: i32, b: i32) -> i32 {
a - b
}
}
fn main() {
let sum = math::add(5, 3);
let difference = math::subtract(5, 3);
println!("Sum: {}", sum);
println!("Difference: {}", difference);
}
Modules in Separate Files
For larger projects, it's common to place modules in separate files. By convention, the module file is placed in a directory named after the module, with a mod.rs
file inside it, or directly in a file named after the module.
- Directory with
mod.rs
:
src/
โโโ main.rs
โโโ math/
โโโ mod.rs
- src/main.rs:
mod math;
fn main() {
let sum = math::add(5, 3);
let difference = math::subtract(5, 3);
println!("Sum: {}", sum);
println!("Difference: {}", difference);
}
- src/math/mod.rs:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn subtract(a: i32, b: i32) -> i32 {
a - b
}
- Direct File Module:
src/
โโโ main.rs
โโโ math.rs
- src/main.rs:
mod math;
fn main() {
let sum = math::add(5, 3);
let difference = math::subtract(5, 3);
println!("Sum: {}", sum);
println!("Difference: {}", difference);
}
- src/math.rs:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn subtract(a: i32, b: i32) -> i32 {
a - b
}
Submodules
Modules can also contain submodules. This helps in further organizing the code.
src/
โโโ main.rs
โโโ math/
โโโ mod.rs
โโโ advanced.rs
- src/main.rs:
mod math;
fn main() {
let sum = math::add(5, 3);
let difference = math::subtract(5, 3);
let power = math::advanced::power(2, 3);
println!("Sum: {}", sum);
println!("Difference: {}", difference);
println!("Power: {}", power);
}
- src/math/mod.rs:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn subtract(a: i32, b: i32) -> i32 {
a - b
}
pub mod advanced;
- src/math/advanced.rs:
pub fn power(base: i32, exponent: i32) -> i32 {
(0..exponent).fold(1, |acc, _| acc * base)
}
Re-exports
To simplify the module structure, you can re-export items from submodules in the parent module.
- src/math/mod.rs:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn subtract(a: i32, b: i32) -> i32 {
a - b
}
pub mod advanced {
pub fn power(base: i32, exponent: i32) -> i32 {
(0..exponent).fold(1, |acc, _| acc * base)
}
}
pub use advanced::power;
Now, you can access power
directly from the math
module:
mod math;
fn main() {
let sum = math::add(5, 3);
let difference = math::subtract(5, 3);
let power = math::power(2, 3);
println!("Sum: {}", sum);
println!("Difference: {}", difference);
println!("Power: {}", power);
}
Summary
- Modules: Used to organize code into smaller, manageable pieces.
- Inline Modules: Defined within the same file using the
mod
keyword. - Separate File Modules: Defined in separate files or directories with a
mod.rs
file. - Submodules: Modules can contain other modules, helping in further organizing the code.
- Re-exports: Simplify module structures by re-exporting items.
Modules in Rust help in structuring and organizing code, making it more readable and maintainable.