首页 文章

重构到工作空间结构会导致extern crate导入无效

提问于
浏览
0

我需要我的项目的不同部分使用相同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 /根/ Cargo.toml:

[package]
name = "root"
version = "0.1.0"
authors = ["Phoenix <kahlo.phoenix@gmail.com>"]

[dependencies]
time = "0.1"

workspace_test /根/ SRC / main.rs:

pub extern crate time;

fn main() {
    println!("Hello, world!");
}

This is also on github,所以它很容易被克隆并且 cargo run 'd .

这是错误:

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`.

1 回答

  • 1

    workspace_test/Cargo.toml 中,您使用二进制 root 创建一个包 . 如果执行 cargo run ,它将运行 main.rs ,但由于您未在此清单文件中声明依赖项,因此会发生错误 . 依赖关系仅在 workspace_test/root/Cargo.toml 中指定,此时不使用 .

    我假设你想使用RFC提出的工作空间 . 您可以使用虚拟清单创建工作区,该清单既不能指定_1447100也不能指定 [[bin]] ,因此只需删除它们即可 . workspace_test/Cargo.toml 现在看起来像这样:

    [workspace]
    members = [
        "root"
    ]
    

    如果您只有一个可执行文件,则现在可以传递该包: -p/--package

    cargo run -p root
    

    或手动指定清单路径:

    cargo run --manifest-path root/Cargo.toml
    

    如果 root/Cargo.toml 包含多个目标,您可以像往常一样附加 --lib--bin 标志 . 例如 . 这将执行 workspace_test/root/Cargo.toml 中指定的 abc -binary:

    cargo run -p root --bin abc
    

相关问题