我需要项目的不同部分使用同一个 extern crate 的不同版本,所以我正在重构我的 Rust 项目,以便使用 this 通过工作区系统将其分成多个包。作为指南。这样做会导致我所有的 pub extern crate 导入都不起作用。
这篇文章与我最近创建然后删除的文章非常相似 - 这个版本包含一个最小的、完整的和可验证的示例。
这是我的项目结构
workspace_test/
root/
src/
main.rs
Cargo.toml
Cargo.toml
workspace_test/Cargo.toml:
[package]
name = "workspace_test"
version = "0.1.0"
authors = ["Phoenix <kahlo.phoenix@gmail.com>"]
[workspace]
members = [
"root"
]
[[bin]]
name = "root"
path = "root/src/main.rs"
workspace_test/root/Cargo.toml:
[package]
name = "root"
version = "0.1.0"
authors = ["Phoenix <kahlo.phoenix@gmail.com>"]
[dependencies]
time = "0.1"
workspace_test/root/src/main.rs:
pub extern crate time;
fn main() {
println!("Hello, world!");
}
This is also on github ,因此它可以很容易地被克隆和cargo run
。
这是错误:
error[E0463]: can't find crate for `time`
--> root/src/main.rs:1:1
|
1 | pub extern crate time;
| ^^^^^^^^^^^^^^^^^^^^^^ can't find crate
error: aborting due to previous error
error: Could not compile `workspace_test`.
最佳答案
在 workspace_test/Cargo.toml
中,您使用二进制 root
创建一个包。如果您执行 cargo run
,它会运行 main.rs
,但由于您没有在此 list 文件中说明依赖项,因此会发生错误。依赖仅在 workspace_test/root/Cargo.toml
中指定,此时未使用。
我假设您想使用 RFC 提议的工作区。您可以创建一个带有虚拟 list 的工作区,它既不能指定 [package]
也不能指定 [[bin]]
,因此只需删除它们即可。 workspace_test/Cargo.toml
现在看起来像这样:
[workspace]
members = [
"root"
]
如果你只有一个可执行文件,你现在可以传递包:-p/--package
cargo run -p root
或手动指定 list 路径:
cargo run --manifest-path root/Cargo.toml
如果 root/Cargo.toml
包含多个目标,您可以照常附加 --lib
或 --bin
标志。例如。这将执行 abc
- 在 workspace_test/root/Cargo.toml
中指定的二进制文件:
cargo run -p root --bin abc
关于rust - 重构工作区结构会导致 extern crate 导入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49712872/