I want to split my code into multiple subdirectories of src. Example:
src
main.rs
sorting_algorithms
bubble.rs
bubble.rs contains a function bubble_sort
; how do I import it to the main.rs?
I want to split my code into multiple subdirectories of src. Example:
src
main.rs
sorting_algorithms
bubble.rs
bubble.rs contains a function bubble_sort
; how do I import it to the main.rs?
The subfolder must be declared as a module. You can do that using 3 different ways:
Inline: declare the sorting_algorithms
module inside your main.rs
:
// In main.rs:
mod sorting_algorithms {
pub mod bubble;
}
This is the simplest in my opinion.
Put a sorting_algorithms.rs
into the src
folder, with the module declaration:
// In sorting_algorithms.rs:
pub mod bubble;
Put a mod.rs
file with the above content into the subfolder. This is advised against, because it can be confusing to have several mod.rs
file to work with.
sorting_algorithms::bubble::sort
or you import it in scope: use sorting_algorithms::bubble::sort;
and then you can use sort
directly. –
Cleaner mod.rs
is deprecated? it will stop working from some version or is considered a bad practice for some reason? –
Padauk __init__.py
–
Padauk Rust will recognize a subfolder of src as a module only if you add a mod.rs
file to it. Add it to the sorting_algorithms folder:
src
main.rs
sorting_algorithms
bubble.rs
mod.rs
The mod.rs file can expose a submodule of this folder:
pub mod bubble;
Assuming the function bubble_sort
is declared public (pub fn bubble_sort(...)
) you will be able to use it from main.rs:
mod sorting_algorithms;
pub use sorting_algorithms::bubble::bubble_sort;
Modules in Rust help you structure your code in a clear and logical way.
Let's assume you have a file structure in your project like this:
.
├── Cargo.toml
└── src
├── main.rs
├── subfolder1
│ ├── file1.rs
│ ├── file2.rs
│ └── subsubfolder
│ └── file1.rs
└── subfolder2
└── file1.rs
And you want to use the functions defined in the files inside the subfolders from your main.rs
file.
To do this, you can declare the subfolders as modules directly in your main.rs
file by using the mod
keyword with curly braces, and list the sub-modules inside:
// This is src/main.rs
mod subfolder1 {
pub mod subsubfolder {
pub mod file1;
}
pub mod file1;
pub mod file2;
}
mod subfolder2 {
pub mod file1;
}
To import the functions from the sub-modules, you need to use the use
keyword with the full module path from the root:
// This can be any file
use crate::subfolder1::file1::fn1;
use crate::subfolder1::file2::fn2;
use crate::subfolder1::subsubfolder::file1::nestedfn;
use crate::subfolder2::file1::otherfn;
This method has some advantages:
mod.rs
within a project.And possible drawbacks:
pub mod
keywords and curly braces, which can make it hard to read and edit.In conclusion, this method is one of the ways to use modules in Rust without creating any extra files. It has some benefits and drawbacks, depending on your preference and project structure. You can choose the one that suits your needs best, or try other methods, such as creating a file with the same name as the module (subfolder.rs
), or creating a mod.rs
file inside the sub-folder.
© 2022 - 2024 — McMap. All rights reserved.