imatahi
imatahi

Reputation: 651

Unable to declare a module "cannot declare a new module at this location"

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?

Upvotes: 5

Views: 2579

Answers (1)

Steve Klabnik
Steve Klabnik

Reputation: 15529

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.

Upvotes: 7

Related Questions