在多个文件中使用模块时无法编译项目:"import只能引用通过--extern传递的extern crate名称"

Dan*_*Guy 5 module rust

我在src目录下有main.rsbear.rs.每次编译时,编译器都指向E0658.我已经阅读了E0658的文档,它告诉我这是一个不稳定的功能.

main.rs

mod bear;

use bear::factory::make_bear;

fn main() {
    println!("Hello, world!");
    let bear = make_bear();
}
Run Code Online (Sandbox Code Playgroud)

bear.rs

pub mod factory {
    pub fn make_bear() -> Bear {
        // code to instantiate Bear struct.
    }    
}
Run Code Online (Sandbox Code Playgroud)

当我编译这段代码时,我从编译器得到这个:

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`
   |
Run Code Online (Sandbox Code Playgroud)

我是否必须等待Rust社区的共识,或者除了文档中不方便的建议之外,我现在能做些什么吗?

ste*_*tew 10

更改

use bear::factory::make_bear;
Run Code Online (Sandbox Code Playgroud)

use crate::bear::factory::make_bear;
Run Code Online (Sandbox Code Playgroud)

这是2018版Rust的变化.我不会在这个页面上重新创建所有内容,但我可以说这个变化的动机是双重的,一个是停止要求extern crate bear;指令,同时也消除了在你有两个本地模块名称的情况下可能出现的歧义bear还有一个外部箱子的依赖也命名bear.

  • 祝贺10k. (3认同)