[英]cargo run: can't find crate even though it's listed in Cargo.toml
I am using a Rust script called build.rs
for code generation (it's not a Cargo build script).我正在使用名为build.rs
的 Rust 脚本来生成代码(它不是 Cargo 构建脚本)。 The script contains this at the top:该脚本在顶部包含以下内容:
extern crate reqwest;
This is my Cargo.toml
file:这是我的Cargo.toml
文件:
[package]
name = "my-script"
version = "0.1.0"
edition = "2018"
[[bin]]
path = "build.rs"
name = "builder"
[dependencies]
reqwest = { version = "0.10", features = ["blocking", "json"] }
When I do cargo run
, it gives me this error:当我执行cargo run
,它给了我这个错误:
error[E0463]: can't find crate for `reqwest`
--> build.rs:1:1
|
1 | extern crate reqwest;
| ^^^^^^^^^^^^^^^^^^^^^ can't find crate
How do I make it able to find the reqwest
crate?我如何让它能够找到reqwest
板条箱?
build.rs
is the default name for a Cargo build script , which is ran at build time to generate code, link in C libraries, etc. build.rs
是Cargo 构建脚本的默认名称,它在构建时运行以生成代码、C 库中的链接等。
Since cargo sees that you have a build.rs
in your project directory, it assumes its a build script and tries to build it.由于cargo 看到您的项目目录中有一个build.rs
,它假定它是一个构建脚本并尝试构建它。 However build scripts don't use the normal dependencies and instead use dependencies from the [build-dependencies]
section of Cargo.toml
.然而,构建脚本不使用普通的依赖项,而是使用Cargo.toml
的[build-dependencies]
部分的[build-dependencies]
Cargo.toml
。 Since you don't have one, the reqwest
crate isn't available to the build script.由于您没有,因此reqwest
箱不适用于构建脚本。
Renaming the file is an obvious fix.重命名文件是一个明显的修复。 However the convention is to put executables into the bin/
directory of your project (cargo will even automatically find files in there for you).但是约定是将可执行文件放入项目的bin/
目录中(cargo 甚至会自动为您在其中查找文件)。
The build.rs
script needs a different name. build.rs
脚本需要一个不同的名称。 Rename build.rs
to builder.rs
and update Cargo.toml
:将build.rs
重命名为builder.rs
并更新Cargo.toml
:
path = "builder.rs"
You can also remove extern crate reqwest
statement because it's inferred by Cargo.您还可以删除extern crate reqwest
语句,因为它是由 Cargo 推断的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.