我有4个文件:
main.rs
mod bar;
fn main() {
let v = vec![1, 2, 3];
println!("Hello, world!");
}
lib.rs
pub mod foo;
pub mod bar;
foo.rs
pub fn say_foo() {
}
bar.rs
use crate::foo;
fn bar() {
foo::say_foo();
}
当我运行 cargo run
时,我得到一个错误:
error[E0432]: unresolved import `crate::foo`
--> src/bar.rs:1:5
|
1 | use crate::foo;
| ^^^^^^^^^^ no `foo` in the root
有人能给我解释一下怎么修吗?更广泛一点: 当有一个 main.rs
和一个 lib.rs
时,模块查找是如何工作的?
编辑: 将 mod foo
添加到 main.rs
修复了这个问题。但是我不明白——我以为 lib.rs
是“暴露”我所有模块的地方?为什么我还必须在 main.rs
中声明模块?
我的 Cargo.toml
:
[package]
name = "hello-world"
version = "0.1.0"
authors = ["me@mgail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]