I've just started learning rust and having trouble including files.
So my problem is that when I want to use a struct
from a.rs
in b.rs
the only way I got it to work is by the absolute path. So use crate::stuff::a::StructA;
. From what I've read, I should be able use mod
here because it's in the same module.
To get to my question, for someone who has a background in c/c++ and python how I should include things correctly? (Because this absolute path really feels inconvenient.)
Directory structure:
src
├── stuff
│ ├── a.rs
│ ├── b.rs
│ └── c.rs
├── stuff.rs
└── main.rs
b.rs
:
use crate::stuff::a::StructA;
/* doesn't work
mod stuff;
use stuff::a::StructA;
*/
/* doesn't work
mod a;
use a::StructA;
*/
// Works but why should I define the path. It's in the same dir/mod.
#[path = "a.rs"]
mod a;
use a::StructA;
stuff.rs
:
pub mod a;
pub mod b;
pub mod c;
main.rs
:
use crate::stuff::a::StructA;
use crate::stuff::b::StructB;
use crate::stuff::c::StructC;
fn main() {
let a = StructA::new();
let b = StructB::new(a);
let c = StructC::new(a, b);
}
b.rs
and c.rs
uses parts of a.rs
. main.rs
uses a.rs
, b.rs
and c.rs
.
Edit:
I've also read that usage of mod.rs
is not recommended.