Skip to main content
ubuntuask.com

Back to all posts

How to Import Modules From Another Folder In Rust?

Published on
3 min read
How to Import Modules From Another Folder In Rust? image

Best Rust Programming Guides to Buy in October 2025

1 The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

BUY & SAVE
$30.13 $49.99
Save 40%
The Rust Programming Language, 2nd Edition
2 Programming Rust: Fast, Safe Systems Development

Programming Rust: Fast, Safe Systems Development

BUY & SAVE
$43.99 $79.99
Save 45%
Programming Rust: Fast, Safe Systems Development
3 Rust for Rustaceans: Idiomatic Programming for Experienced Developers

Rust for Rustaceans: Idiomatic Programming for Experienced Developers

BUY & SAVE
$29.99 $49.99
Save 40%
Rust for Rustaceans: Idiomatic Programming for Experienced Developers
4 Rust in Action

Rust in Action

BUY & SAVE
$51.42 $59.99
Save 14%
Rust in Action
5 Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

BUY & SAVE
$47.06 $59.95
Save 22%
Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)
6 Zero To Production In Rust: An introduction to backend development

Zero To Production In Rust: An introduction to backend development

BUY & SAVE
$49.99
Zero To Production In Rust: An introduction to backend development
7 The Rust Programming Language

The Rust Programming Language

BUY & SAVE
$16.92 $39.95
Save 58%
The Rust Programming Language
8 Rust Atomics and Locks: Low-Level Concurrency in Practice

Rust Atomics and Locks: Low-Level Concurrency in Practice

BUY & SAVE
$33.13 $55.99
Save 41%
Rust Atomics and Locks: Low-Level Concurrency in Practice
+
ONE MORE?

To import modules from another folder in Rust, you can use the mod keyword followed by the path to the module file. For example, if you have a module named my_module in a folder named utils, you can import it in your main file using mod utils::my_module. Make sure to specify the path correctly to access the module from another folder in your Rust project.

What is the purpose of the mod keyword in Rust?

In Rust, the mod keyword is used to declare a module, which is a collection of functions, types, and other items that can be used together. Modules help organize code into logical units, making it easier to manage and maintain. By using the mod keyword, developers can create separate modules for different parts of their program and structure their code in a more modular and scalable way.

What is the significance of the visibility modifier in Rust?

In Rust, the visibility modifiers pub, pub(crate), and pub(super) are used to control the visibility of functions, structs, enums, and other items within a module or crate. This allows developers to specify which parts of their code are accessible from outside the module or crate, and which parts are internal implementation details.

By using visibility modifiers, developers can enforce information hiding and encapsulation, which are important principles in software development. This helps to prevent unintended use or modification of internal implementation details by external code, and makes it easier to reason about the behavior of a program.

Additionally, visibility modifiers can also be used to expose public APIs while keeping certain parts of the implementation private. This can help developers create more maintainable and reusable code, as they can make changes to the internal implementation without affecting external code that relies on the public API.

What is the impact of module imports on code performance in Rust?

Module imports in Rust do not have a direct impact on code performance. When you import a module in Rust, the compiler includes the necessary code from that module during the compilation process. Once the code is compiled, there is no difference in performance between using code from an imported module versus writing the code directly in the main file.

However, one potential impact on performance could come from importing large or unnecessary modules, which could result in longer compilation times. It is generally good practice to only import the modules that are needed for your program to avoid unnecessary overhead.

Overall, module imports in Rust are primarily for code organization and readability purposes, and should not have a significant impact on code performance.

How to import a module relative to the current module in Rust?

To import a module relative to the current module in Rust, you can use the mod keyword followed by the path to the module file relative to the current module.

For example, if you have a module named my_module in a file named my_module.rs located in the same directory as your current module, you can import it like this:

mod my_module;

If the module file is located in a subdirectory, you can specify the path relative to the current module like this:

mod subdirectory::my_module;

Make sure to follow Rust's module naming conventions and directory structure to organize and import your modules effectively.