[英]cargo run: can't find crate even though it's listed in Cargo.toml
[英]Why do I get "can't find crate" that is listed as a dependency in Cargo.toml when I compile with rustc?
我的 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
我正在做一个 Exercism 练习,所以我构建/运行程序的方式是rustc src/lib.rs
来测试我的解决方案。 问题是因为我没有运行rustc src/main.rs
吗?
当您直接运行rustc
,编译器只知道命令行参数。 它对Cargo.toml
,特别是,因此它不知道在哪里寻找chrono
库。
要使用依赖管理,你必须用 Cargo 编译你的项目 - 只需使用cargo build
/ cargo run
/ cargo test
,一切都应该没问题。 详情见本书。
但是,如果您想(出于某种原因)直接使用rustc
,我建议无论如何都要检查第一个cargo
,使用cargo build --verbose
。 它将显示所有调用的命令,允许您检查要手动定义的可能参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.