11

How to use multiple files in rust?
My main code is in file1.rs. file2.rs runs the main function of file1.rs multiple times, that's why I want to split it into 2 files, to keep my code clean. Than I just want to run the main function of file2.rs in my main.rs file. (I'm using the latest version of rust - 2021)

Folder structure:

├── Cargo.lock  
├── Cargo.toml  
├── src  
│  ├── main.rs  
│  └── file1.rs
|  └── file2.rs
└── target  

main.rs

pub mod file1;
pub mod file2;

pub fn main() {
    file2::main();
}

file2.rs

pub mod file1;

pub fn main() {
    file1::func("Bob");
    file1::func("Alice");
}

file1.rs

pub fn func(name: &str) {
    println!("Hello {}", name.to_string());
}

I get this error message:

file not found for module `file1`
to create the module `file1`, create file "src/file2/file1.rs"
or "src/file2/file1/mod.rs" rustcE0583
hups
  • 133
  • 1
  • 1
  • 5
  • main is what gets run when a file is run as a program, it should not be called directly. See ["Managing Growing Protects"](https://doc.rust-lang.org/stable/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html#managing-growing-projects-with-packages-crates-and-modules) in The Rust Programming Language for how to organize Rust code. – Schwern Dec 11 '21 at 22:54
  • Okay thhanks, I've changed the function name in `file1` to func. Still the same import error... – hups Dec 12 '21 at 13:10
  • 1
    Found the answer: https://stackoverflow.com/questions/46829539/how-to-include-files-from-same-directory-in-a-module-using-cargo-rust. – hups Dec 12 '21 at 13:20
  • Does this answer your question? [How to include files from same directory in a module using Cargo/Rust?](https://stackoverflow.com/questions/46829539/how-to-include-files-from-same-directory-in-a-module-using-cargo-rust) – Chayim Friedman Dec 14 '21 at 08:40

3 Answers3

5

Here you are saying that file2.rs has a module called file1, so your tree should be:

src
 |
  ---- main.rs
  ---- file2.rs
  ---- file2
        |
        ----- file1.rs

Or change it to:

main.rs:

pub mod file1;
pub mod file2;

// ...

file2.rs:

// pub mod file1

// ...
  • I have the same problem. If `file1` is used by also a `file3`, then the first method does not work. However I also can't make the second one working. `func` called from `main.rs` runs perfectly, but using it from `file2` I get "use of undeclared type or module `file1`". (I used "pub mod file1" in main). Do you have any ideas, what could go wrong? – FERcsI Mar 30 '22 at 09:26
  • @FERcsI what exactly is your module tree? – Rafaelplayerxd YT Mar 30 '22 at 17:44
  • I tried the one exactly here. But my one is, that I try to use a heterogeneous collection and I have a Trait in a separate file, which has to be accessible by the collection handler class, as well the derived classes. All in separate files for the sake of maintainability. As a workaround I found `super::file1::func` working (or `use super::file1`). Maybe this is the desired solution. – FERcsI Apr 02 '22 at 06:52
  • 1
    @FERcsI if you are trying to use file1 from file2 (in the context that file1 belongs to `crate`), you need to use super. – Rafaelplayerxd YT Apr 03 '22 at 13:54
  • Thinking of it now, you DECLARED file1 both on main and on file2, if you just want to USE file1 from file2, you can do something like `use super::file1::*;` where super could be crate or you could import file1 or main directly – Rafaelplayerxd YT Jun 21 '22 at 19:50
1

You need to make the main functions public using pub fn main() {...}. Also, the syntax you used to call the file1::main is invalid, you would have to provide actual values like 1 and "foo".

do-no-van
  • 226
  • 2
  • 4
  • Yeah it still doesn't work and yeah sorry for the actual values I changed the question and added the error message. – hups Dec 11 '21 at 21:26
0

You can import from other modules (files) in the same directory:

use super::file2::*;
jnnnnn
  • 3,889
  • 32
  • 37