重构到工作区结构导致 extern crate 导入不起作用 [英] Refactoring to workspace structure causes extern crate imports to not work

查看:55
本文介绍了重构到工作区结构导致 extern crate 导入不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的项目的不同部分来使用同一个 extern crate 的不同版本,所以我正在重构我的 Rust 项目,以便通过工作区系统使用 this 作为指南.这样做会导致我所有的 pub extern crate 导入无法正常工作.

I need different parts of my project to use different versions of the same extern crate so I'm refactoring my Rust project to be divided into multiple packages via the workspaces system using this as a guide. Doing so is causing all my pub extern crate imports to not work.

这篇文章与我最近创建然后删除的文章非常相似 - 此版本包含一个最小、完整且可验证的示例.

This post is very similar to one I created very recently and then deleted - this version contains a minimal, complete, and verifiable example.

这是我的项目结构

workspace_test/
  root/
    src/
      main.rs
    Cargo.toml
  Cargo.toml

workspace_test/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:

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:

workspace_test/root/src/main.rs:

pub extern crate time;

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

这个也在github上,所以很容易被克隆和cargo run'd.

This is also on github, so it can easily be cloned and 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`.

推荐答案

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

In workspace_test/Cargo.toml you create a package with the binary root. If you execute cargo run, it runs the main.rs, but since you didn't state the dependencies in this manifest file, the error occurs. The dependency is only specified in workspace_test/root/Cargo.toml, which is not used at this point.

我假设您想使用 RFC 提议的工作区.您可以创建一个带有虚拟清单的工作区,它既不能指定 [package] 也不能指定 [[bin]],所以只需删除它们.workspace_test/Cargo.toml 现在看起来像这样:

I assume you want to use the workspaces proposed by the RFC. You can create a workspace with virtual manifests, which must neither specify a [package] nor [[bin]], so just remove them. workspace_test/Cargo.toml now looks like this:

[workspace]
members = [
    "root"
]

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

If you only have one executable, you can now pass the package: -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:

If root/Cargo.toml contains multiple targets, you can just append the --lib or --bin flags as usual. E.g. this would execute the abc-binary specified in workspace_test/root/Cargo.toml:

cargo run -p root --bin abc

这篇关于重构到工作区结构导致 extern crate 导入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆