6

I have the main main.rs in the src folder. The main.rs contains the following content.

#[allow(dead_code)]
fn main() {
    println!("Hello, world!");
    hallo();
}

The main.rs in the src/lib folder does not currently have any content.

How can I now use the function "hello" from the test.rs in the main.rs in the src folder? The test.rs contains the following content.

fn hallo(){
    println!("hallo");
}

My folder structure looks like this.

|   Cargo.lock
|   Cargo.toml
|           
+---src
|   |   main.rs
|   |   
|   \---lib
|           main.rs
|           test.rs
```
Zeyecx
  • 73
  • 1
  • 3
  • 2
    This question has been asked many times on this site. See [How do I do a basic import/include of a function from one module to another?](https://stackoverflow.com/questions/26224947/how-do-i-do-a-basic-import-include-of-a-function-from-one-module-to-another-in-r) – Ibraheem Ahmed May 07 '21 at 03:43
  • 1
    I suggest reading this article, it is the best explanation of the module system out there imo: [Rust modules vs files](https://fasterthanli.me/articles/rust-modules-vs-files) – Ibraheem Ahmed May 07 '21 at 03:45

2 Answers2

14

This gets into the Rust module system. I'll try my best to explain it, but the Book does a much better job, so if you're confused, go ahead and check it out here. Basically, each file in Rust (besides main.rs or lib.rs) corresponds to one module in a crate (main.rs and lib.rs are the crate's root). Everything in that file is exposed under that module. To create a module, you add this line to its parent module (or to lib.rs or main.rs, which makes it a submodule under the crate):

mod somemod;

Then, you have two options:

  • Add a file called somemod.rs in the same folder as the file where you put that mod somemod; declaration. Your file structure would look like this:
|
+---main.rs
+---somemod.rs
  • Add a folder called somemod in the same folder as the file with the mod somemod; declaration. This would be used if you wanted somemod to have child modules; you'd use the same two options to create child modules as you did to create the top-level one. Your file structure looks like this:
|
+---main.rs
+---somemod
    |
    +---mod.rs

Then, after that line (and before, if you really wanted), you'd be able to use the somemod namespace to refer to any public member of the somemod module:

mod somemod;

// later on ...
somemod::foo();

You could also use the use keyword to bring a member of somemod into the current namespace:

mod somemod;
use somemod::foo;

// later on ...
foo();

So, in your case, your code would look like this:

main.rs:

mod test;
use test::hallo;

#[allow(dead_code)]
fn main() {
    println!("Hello, world!");
    hallo();
}

test.rs:

pub fn hallo() {
    println!("hallo");
}

Note that in order to be able to reference hallo from outside the module, we had to add the pub keyword in from of fn hallo.

S. Brown
  • 401
  • 3
  • 6
  • Please check for duplicates before writing up an answer. If your answer is distinct in some way, you can post an answer on the duplicate. You help a lot more people that way :) – Ibraheem Ahmed May 07 '21 at 03:45
  • Thanks for your help. I was able to solve my problem. – Zeyecx May 10 '21 at 21:33
2

I am learning rust-lang.
This is what I understood about module imports. Let your project structure be like,

---- src /
      |
      ---- main.rs
---- Cargo.lock
---- Cargo.toml

Now you created a folder called parent along with main.rs like,

---- src /
      |
      ---- main.rs
      ---- parent /
             |
             ---- child /
                    |
                    ---- my_file.rs
---- Cargo.lock
---- Cargo.toml

To import a function my_function from my_file.rs,

Step 1: Create a file called lib.rs along with main.rs
Step 2: Add empty mod.rs in parent and child folders.
Step 3: In lib.rs add pub mod parent
Step 4: In mod.rs of parent, add pub mod child
Step 5: In mod.rs of child, add pub mod my_file
Step 6: In main.rs add mod parent
Step 7: Finally, use it by parent::child::my_file::my_function()

Final project structure will be like,

---- src /
      |
      ---- main.rs                   // mod parent
      ---- lib.rs                    // pub mod parent
      ---- parent /
             |
             ---- mod.rs             // pub mod child
             ---- child /
                    |
                    ---- mod.rs      // pub mod my_file
                    ---- my_file.rs
---- Cargo.lock
---- Cargo.toml

Hopes this will help.

Henshal B
  • 1,540
  • 12
  • 13