13

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
4

3 Answers 3

7

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

// ...
Sign up to request clarification or add additional context in comments.

5 Comments

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 what exactly is your module tree?
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 if you are trying to use file1 from file2 (in the context that file1 belongs to crate), you need to use super.
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
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".

1 Comment

Yeah it still doesn't work and yeah sorry for the actual values I changed the question and added the error message.
1

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

use super::file2::*;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.