4

This may be a stupid question, but I cannot seem to solve this.

I have this kind of file structure:

└── src
    ├── another.rs
    ├── some_file.rs
    └── main.rs

In the some_file.rs, I want to call a function in the main.rs. So, I tried to do something like this in the some_file.rs:

use crate::main


fn some_func() {
   // other code
   
   main::another_func_in_main();
}

But the compiler throws an error:

use of an undeclared crate or module `main`

How can I solve this?

Herohtar
  • 5,347
  • 4
  • 31
  • 41
bichanna
  • 954
  • 2
  • 21

1 Answers1

6

There is no main module, even though you have a main.rs file. The stuff you put in the main.rs file are considered to be at the root of the crate.

So you have two ways to call the function:

1. Directly (w/o use)

crate::another_func_in_main();

2. By importing it first

use crate::another_func_in_main;

// Then in code, no need for a prefix:
another_func_in_main();
at54321
  • 8,726
  • 26
  • 46
  • 2
    Commenting to link https://stackoverflow.com/q/20922091/3650362 and https://stackoverflow.com/q/61244940/3650362 (which points out you can also use `super::` for module paths relative to the current module) – trent Jan 31 '22 at 16:25
  • @WarrenWeckesser You're right, thank you! – at54321 Jan 31 '22 at 19:20