How to import from a file in a subfolder of src?
Asked Answered
P

3

16

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?

Padauk answered 19/11, 2019 at 14:5 Comment(0)
C
24

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.

Cleaner answered 19/11, 2019 at 14:16 Comment(6)
Using the second method... how do you import it to the main.rs?Berghoff
@Berghoff There is no difference between the 3 methods: either you use the full name sorting_algorithms::bubble::sort or you import it in scope: use sorting_algorithms::bubble::sort; and then you can use sort directly.Cleaner
Can you elaborate on why mod.rs is deprecated? it will stop working from some version or is considered a bad practice for some reason?Padauk
@Padauk It's not the best wording: it's not deprecated in the sense that it'll be unsuported. It's more that the team advices against using it.Cleaner
got it. But why they advise against it? to me it seems nice that I don't have to put anything outside the folder, like __init__.pyPadauk
@Padauk I updated my answer. If you want everything to be self-contained, you should use the inline solution, IMHO.Cleaner
P
10

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;
Padauk answered 19/11, 2019 at 14:5 Comment(0)
G
0

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:

  • It allows subfolders. You can organize your code however you like.
  • It is simple and concise. You don't need to create any extra files.
  • It reflects the file structure. You can easily see which modules you have and navigate to them.
  • It is consistent with the Rust conventions. The Rust reference advices against having many files named mod.rs within a project.

And possible drawbacks:

  • It can make your main.rs file too long and cluttered. If you have many subfolders and files, your main.rs file will have a lot of pub mod keywords and curly braces, which can make it hard to read and edit.
  • It requires updating main.rs frequently. If you add a new module in a subfolder, you need to edit your main.rs file and declare it with the mod keyword, otherwise it will not be recognized by the compiler. This can be tedious, especially if you have many files to manage.

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.

Genotype answered 27/6, 2023 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.