【问题标题】:Why do I get "can't find crate" that is listed as a dependency in Cargo.toml when I compile with rustc?当我使用 rustc 编译时,为什么会出现在 Cargo.toml 中列为依赖项的“找不到 crate”?
【发布时间】:2020-03-04 21:59:35
【问题描述】:

我的 Cargo.toml 包括:

[dependencies]
chrono = "0.4"

我的代码包括这个:

extern crate chrono;
use chrono::{Duration, DateTime, Utc};

但是当我运行我的代码时,我得到了这个错误:

error[E0463]: can't find crate for `chrono`
 --> src/lib.rs:1:1
  |
1 | extern crate chrono;
  | ^^^^^^^^^^^^^^^^^^^^ can't find crate

我正在做一个练习练习,所以我构建/运行程序的方式是rustc src/lib.rs 来测试我的解决方案。问题是因为我没有运行rustc src/main.rs吗?

【问题讨论】:

    标签: rust rust-cargo


    【解决方案1】:

    当您直接运行rustc 时,编译器只知道命令行参数。它不知道任何关于Cargo.toml 的信息,尤其是,所以它不知道去哪里寻找chrono 库。

    要使用依赖管理,您必须使用 Cargo 编译您的项目 - 只需使用 cargo build/cargo run/cargo test,一切都应该没问题。详情请见the Book

    但是,如果您想(出于某种原因)直接使用rustc,我建议您首先检查cargo,使用cargo build --verbose。它将显示所有被调用的命令,允许您检查要手动定义的可能参数。

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多