Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't compile project when using modules in multiple files: "imports can only refer to extern crate names passed with --extern"

Tags:

module

rust

I have main.rs and bear.rs under the src directory. Every time I compile, the compiler points to E0658. I've read the docs for E0658, and it tells me that this is an unstable feature.

main.rs

mod bear;

use bear::factory::make_bear;

fn main() {
    println!("Hello, world!");
    let bear = make_bear();
}

bear.rs

pub mod factory {
    pub fn make_bear() -> Bear {
        // code to instantiate Bear struct.
    }    
}

When I compile this code I get this from the compiler:

error[E0658]: imports can only refer to extern crate names passed with `--extern` on stable channel (see issue #53130)
  --> src/main.rs:1:5
   |
1  |   use bear::factory::make_bear;
   |       ^^^^
...
8  | / mod bear {
9  | |     pub mod factory {
10 | |         pub fn make_bear() -> Bear {
11 | |             // code to instantiate Bear struct.
12 | |         }
13 | |     }
14 | | }
   | |_- not an extern crate passed with `--extern`
   |

Do I have to wait for consensus among the Rust community, or is there something I can do right now besides the inconvenient suggestion in the docs?

like image 742
DanielGuy Avatar asked Dec 26 '18 21:12

DanielGuy


1 Answers

Change

use bear::factory::make_bear;

to

use crate::bear::factory::make_bear;

This was a change in the 2018 edition of Rust. I won't recreate everything on this page, but I can say that the motivation for this change is twofold, one is to stop requiring the extern crate bear; directives, while also removing the ambiguities that might arise in the case that you have both a local module name bear and also a dependency on an external crate also named bear.

like image 63
stew Avatar answered Nov 02 '22 16:11

stew