Importing in Rust

Viewed 640

I'm a newbie in Rust.

I have this file structure now:

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

In the some_file file, I have this code:

mod another;

// and code

However, the compiler throws an error and offers this help:

 = help: to create the module `another`, create file "src/some_file/another.rs" or "src/some_file/another/mod.rs"

But I don't want to do so. I want both files to be in the same directory. Are there any ways I can avoid creating a new directory?

1 Answers

Assuming you have a binary project, you need to have src/main.rs. In your main.rs if you put mod another, Rust will be looking for src/another.rs and it will work as expected.

If you have this structure:

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

and want to use another in some_file, then you shouldn't use mod in some_file, but only import it via use crate::another;.

Related