按照本指南,我創建了一個貨運項目。src/main.rsfn main() { hello::print_hello();}mod hello { pub fn print_hello() { println!("Hello, world!"); }}我運行使用cargo build && cargo run并且編譯沒有錯誤?,F在,我試圖將主模塊一分為二,但無法弄清楚如何從另一個文件中包含一個模塊。我的項目樹看起來像這樣├── src ├── hello.rs └── main.rs以及文件的內容:src/main.rsuse hello;fn main() { hello::print_hello();}src/hello.rsmod hello { pub fn print_hello() { println!("Hello, world!"); }}當我編譯它cargo build,我得到error[E0432]: unresolved import `hello` --> src/main.rs:1:5 |1 | use hello; | ^^^^^ no `hello` external crate我試圖遵循編譯器的建議,并修改main.rs為:#![feature(globs)]extern crate hello;use hello::*;fn main() { hello::print_hello();}但這仍然無濟于事,現在我明白了:error[E0463]: can't find crate for `hello` --> src/main.rs:3:1 |3 | extern crate hello; | ^^^^^^^^^^^^^^^^^^^ can't find crate是否有一個簡單的示例,說明如何將當前項目中的一個模塊包含到項目的主文件中?另外,我正在運行Rust 1.37.0。
- 2 回答
- 0 關注
- 856 瀏覽
添加回答
舉報
0/150
提交
取消