Rust Lang

Rust Import from Another File

One of the critical features of Rust is its support for modular development which allows us to break the code into smaller, reusable components.

In this tutorial, we will explore how to import the files in Rust which is a crucial concept for modular development, and how it can help to improve the code organization, reusability, and maintainability.

We will cover the different ways to import the files in Rust including the mod keyword, the use keyword, and the pub keyword. By the end of this tutorial, you will better understand Rust’s modular development capabilities and how to leverage them to write more efficient and maintainable code.

Requirements:

Before we start, ensure that you have Rust and Cargo installed on your machine. You can download Rust and Cargo from the official Rust website at https://www.rust-lang.org/tools/install. It would help if you also understood Rust’s syntax, types, and control structures.

Method 1: Import a Module from a File Using the Mod Keyword

Importing from files is one of the most fundamental methods of modular development. It allows us to separate the code into various components where each file is responsible for a specific task or feature.

We can then import the needed components to build an application. This makes the code easy to maintain, debug, and improve.

In Rust, we import the code from a file using the “mod” keyword which is also known as a module. A module refers to a collection of related code which is organized into a file or a group of files.

To import a module from a file, we need to create a file with the same name as the module and put it in a directory with the same name as the crate (the root module).

To demonstrate, let us start by creating a new file called “math_ops.rs”. This file holds the code for most fundamental math operations.

The source code is as follows:

pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

pub fn sub(a: i32, b: i32) -> i32 {
    a - b
}

pub fn prod(a: i32, b: i32) -> i32 {
    a * b
}

pub fn div(a: i32, b: i32) -> i32 {
    a / b
}

 
The previous file contains four main functions that accept the integer values and perform addition, subtraction, multiplication, and division on the provided values.

To use the code that is defined in this file, we need to use the “mod” keyword. For example, we want to import one of the functions in a “main.rs” file. We can use the “mod” keyword followed by the file’s name:

mod math_ops;

fn main() {
    let result = math_ops::add(1, 2);
    println!("1 + 2 = {}", result);

    let result = math_ops::sub(5, 3);
    println!("5 - 3 = {}", result);

    let result = math_ops::prod(25, 33);
    println!("25 * 33 = {}", result);

    let result = math_ops::div(15, 3);
    println!("15 / 3 = {}", result);
}

 
In the previous example, we use the functions from the math module in our main function.

Note:  We need to declare the math module as public using the pub keyword in the “math_ops.rs” file to make it accessible from other files.

If we don’t declare it publicly, Rust fails to import the code into other files.

Method 2: Import a Function from a File Using the Use Keyword

Rust also allows us to use the “use” keyword to access other functions and data types from a module. The “use” keyword removes the need to type the full module path when using it.

Suppose we have a file called “greetings.rs” with the code as shown in the following:

pub fn greet(name: &str) {
    println!("Hello, {}!", name);
}

 
To import the function in another file, we can use the “use” keyword as shown in the following:

use crate::greetings::greet;
fn main() {
    greet("world");
}

 
In this case, we use the “use” keyword to bring the greet function from the greetings module into the scope.

Conclusion

We explored how to import the files in Rust, an essential concept for modular development. We learned how to import a module using the “mod” keyword, make a module public using the “pub” keyword, and import a function using the “use” keyword in Rust.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list