Rust Interview Questions

35 Questions
Rust

Rust

Web DevelopmentIoT

Question 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.

  1. 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
}
  1. 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.

Recent job openings