0

I'm stuck when learning how to access a module. I'm trying to insert a folder other than src into src. It's not working and it gives me an error. Here this is my project tree.

$ Project1
.
|-- src
|       |-- main.rs
|   |--FolderinSrcFolder 
|       |--folderinsrcmodule.rs    
|
|--anothersrc
|   |--mod.rs
|
|-- rootmodule.rs
|-- Cargo.toml
|-- Cargo.lock

How can I access anothersrc/mod.rs src/main.rs? How can I access rootmodule.rs from src/main.rs?

I already read the Rust documentation.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

1 Answers1

6

Idiomatic solution

Don't. Put all of your source code into the src directory. You could also create another crate with its own src directory. Don't fight these idioms and conventions, it's simply not worth it.

See also:

Literal solution

This directly answers your question, but I strongly recommend that you don't actually use this!

Layout

.
├── Cargo.toml
├── bad_location.rs
└── src
    └── main.rs

src/main.rs

#[path = "../bad_location.rs"]
mod bad_location;

fn main() {
    println!("Was this a bad idea? {}", bad_location::dont_do_this());
}

badlocation.rs

pub fn dont_do_this() -> bool {
    true
}

The key is the #[path] annotation.

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366