I have 3 files: lib.rs, file2.rs and file3.rs. I lib.rs I have this:
mod file2;
use file2::Struct2;
and it's working well. However, doing the same thing in file3 compiles with an error:
mod file2;
use file2::Struct2;
=> error: cannot declare a new module at this location
And if I remove mod file2
declaration I get this:
error: unresolved import `Struct2`
What's wrong with this?
I'm not sure why you're getting that error exactly, but that's not going to do what you want. Modules form a tree structure, and you use mod declarations to form them. So you're trying to create another file2 mod inside the file3 one.
I'm guessing you want file2 and file3 to both be under the main module, not sub modules of each other. To do this, put
mod file2;
mod file3;
In lib.rs, and then in file3.rs
use file2::Struct2;
And it should all work. I'm on mobile, so I can't triple check myself, sorry about formatting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With