【问题标题】:Local crate in RustRust 中的本地板条箱
【发布时间】:2020-04-01 10:28:03
【问题描述】:

我目前正在学习 rust,为此我想创建自己的 crate 并使用它。 但是rust找不到这个箱子。 我有以下文件结构:

├───minimal
│   ├───Cargo.toml
│   └───src
│       └───main.rs
└───util
    └───win
        ├───Cargo.toml
        └───src
            └───lib.rs

在文件夹minimal 我有我的主要项目。它看起来像这样:

Cargo.toml

[package]
name = "minimal"
version = "0.1.0"
[dependecies]
win = { path = "../util/win"}
main.rs 

extern crate win; // ERROR: "Can't find crate for 'win' rustc(E0463)"
fn main() {
    println!("Hello World!");
}

我在文件夹 win 中的库如下所示:

Cargo.toml

[package]
name = "win"
version = "0.1.0"
lib.rs

pub type TestType = String;

我的第一个假设是我在指定 Cargo.toml 依赖项中的路径时出现了错误。所以我试着把它扭动了一下,但它似乎不起作用。 Rust 报告 can't find crate for 'win' rustc(E0463)

我觉得我在这里犯了一个非常基本的错误,但是在查看类似问题时(例如How do I "use" or import a local Rust file?)我似乎找不到它。

【问题讨论】:

  • 您是否尝试过使用绝对路径来查看是否可行?
  • 我试过了,是的 - 不幸的是它没有用。我是否需要以特定方式编译我的库才能使其正常工作?
  • 不要这么认为...我只是复制了您的设置,但无法得到相同的错误。另一件要尝试的事情可能是使用 2018 版,尽管我怀疑它是否重要。
  • 只是为了检查一下,您正在使用cargo 来构建/运行它吗? IE。在最小目录中运行cargo run
  • 我使用 cargo run 来运行它,是的。我从这里安装了 rust:rust-lang.org/tools/install 并从 Windows 10 中的 Visual Studio 代码运行它。(如果我使用cmd-prompt 直接。)奇怪的是它对你来说是开箱即用的。

标签: rust local crate


【解决方案1】:

睡了个好觉,再次查看这个问题后,我设法找到了错误。
我在 Cargo.toml 中使用了 [dependecies] 而不是 [dependencies]

一方面我对这个错误感到有点愚蠢,另一方面我现在知道 cargo 不会警告 toml 文件中的未知标签。

【讨论】:

  • 我喜欢你对结果的感觉。清爽健康。
猜你喜欢
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2021-05-29
  • 2023-01-20
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多