无法在 Rust 中修改模块

问题描述 投票:0回答:1

这是我的项目结构:

.
├── Cargo.toml
└── src
    ├── allocator.rs
    ├── config.rs
    ├── fs.rs
    ├── lib.rs
    └── main.rs

2 directories, 6 files

我的代码:

// src/lib
mod allocator;
use allocator::*;
// src/allocator
pub fn function(){

}

然后编译失败:

error[E0583]: file not found for module `allocator`
  --> pkg/boot/src/lib.rs:23:1
   |
23 | mod allocator;
   | ^^^^^^^^^^^^^^
   |
   = help: to create the module `allocator`, create file "pkg/boot/src/lib/allocator.rs" or "pkg/boot/src/lib/allocator/mod.rs"
   = note: if there is a `mod allocator` elsewhere in the crate already, import it with `use crate::...` instead

我已经尝试了建议的解决方案,但仍然失败。我不确定我是否做错了什么。

rust compiler-errors rust-cargo
1个回答
0
投票

请仔细阅读编译器错误,它告诉您它试图在哪里寻找

allocator.rs
src/lib/allocator.rs
不是
src/allocator.rs
)这意味着您在某处有一个
mod lib;
,除非您在其中明确给出了
[library]
Cargo.toml
不同的
path
是错误的(即便如此,我强烈建议反对)。

您可以通过使用板条箱名称来使用

lib.rs
中的
main.rs
部分:
use boot::function;

© www.soinside.com 2019 - 2024. All rights reserved.