-1

I have three files:

// a.rs

struct MyThing {
}
// b.rs

mod a;

struct That {
    mything: &a::MyThing;
}
// main.rs

mod a;
mod b;

fn main() {
    let thing= a::MyThing{};
    let that= b::That{myThing: &thing};
}

The compile error I get for a.rs is:

file not found for module b help: to create the module b, create file "src/a/b.rs" or "src/a/b/mod.rs"

I thought I would need mod a; so that I can access the module in a.rs, but it looks like since mod b; is in main.rs, the mod a; inside b.rs is interpreted relative to b...or something.

How do I use one .rs file from another?

lmat - Reinstate Monica
  • 7,289
  • 6
  • 48
  • 62

1 Answers1

-1

mod a; does not just declare or make reference to the module, a, it defines the module, a. Putting mod a; in b.rs creates a separate module from that created in main.rs. Instead, let main.rs create the modules and make reference to a in b. In this case, you need to make reference it according to its crate. In which crate is the module ./b.rs? The root crate, crate:

// b.rs

use crate::a;

struct That {
    mything: &a::MyThing;
}
lmat - Reinstate Monica
  • 7,289
  • 6
  • 48
  • 62