Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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?

like image 968
imatahi Avatar asked Aug 15 '15 04:08

imatahi


1 Answers

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.

like image 128
Steve Klabnik Avatar answered Nov 15 '22 07:11

Steve Klabnik