在 "maybe a missing extern crate" 的 Cargo 项目中使用板条箱出错
Using a crate in a Cargo project errors with "maybe a missing extern crate"
今天开始学习Rust,但是卡在了this step。我想在我的项目中使用 rand crate,所以我按照教程中的建议更新了我的 Cargo.toml
:
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <novice.coder@gmail.com>"]
[dependencies]
rand = "0.3.14"
在我的代码中将其导入为:
use rand::Rng;
它给出了这个错误:
error[E0432]: unresolved import `rand`
--> src/main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing `extern crate rand;`?
我是不是漏掉了什么?
我按照建议添加了edition = "2018"
:
Cargo.toml:
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <novice.coder@gmail.com>"]
edition = "2018"
[dependencies]
rand = "0.3.14"
Cargo 构建现在提供:
$ cargo build --verbose
Fresh libc v0.2.45
Fresh rand v0.4.3
Fresh rand v0.3.22
Compiling guessing_game v0.1.0 (/home/bappaditya/projects/guessing_game)
Running `rustc --edition=2018 --crate-name guessing_game src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=4d1c2d587c45b4
c6 -C extra-filename=-4d1c2d587c45b4c6 --out-dir
/home/bappaditya/projects/guessing_game/target/debug/deps -C
incremental=/home/bappaditya/projects/guessing_game/target
/debug/incremental -L
dependency=/home/bappaditya/projects/guessing_game/target/debug/deps --
extern rand=/home/bappaditya/projects/guessing_game/target/debug/deps/libra
nd-78fc4b142cc921d4.rlib`
error: Edition 2018 is unstable and only available for nightly builds of rustc.
我使用 rustup update
更新了 Rust,然后将 extern crate rand;
添加到我的 main.rs。现在它按预期工作。
程序 运行 但在我的 vscode 问题选项卡中它仍然显示错误 -
error[E0432]: unresolved import `rand`
--> src/main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing `extern crate rand;`?
快速修复是添加
edition = "2021"
到您的 Cargo.toml
,在 [dependencies]
行上方。
说明
Rust 主要版本 三个:Rust 2015、2018 和 2021。新代码建议使用 Rust 2021,但由于 Rust 需要向后兼容,因此您有选择使用它。
在 Rust 2015 中,您必须在使用 std
之外的任何内容之前编写 extern crate
语句。这就是错误消息的来源。但是在 Rust 2018 或 2021 中你不必再这样做了,这就是设置版本修复它的原因。
Rust 2021 有更多变化;如果您有兴趣,可以在 edition guide.
中阅读有关它们的信息
今天开始学习Rust,但是卡在了this step。我想在我的项目中使用 rand crate,所以我按照教程中的建议更新了我的 Cargo.toml
:
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <novice.coder@gmail.com>"]
[dependencies]
rand = "0.3.14"
在我的代码中将其导入为:
use rand::Rng;
它给出了这个错误:
error[E0432]: unresolved import `rand`
--> src/main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing `extern crate rand;`?
我是不是漏掉了什么?
我按照建议添加了edition = "2018"
:
Cargo.toml:
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <novice.coder@gmail.com>"]
edition = "2018"
[dependencies]
rand = "0.3.14"
Cargo 构建现在提供:
$ cargo build --verbose
Fresh libc v0.2.45
Fresh rand v0.4.3
Fresh rand v0.3.22
Compiling guessing_game v0.1.0 (/home/bappaditya/projects/guessing_game)
Running `rustc --edition=2018 --crate-name guessing_game src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=4d1c2d587c45b4
c6 -C extra-filename=-4d1c2d587c45b4c6 --out-dir
/home/bappaditya/projects/guessing_game/target/debug/deps -C
incremental=/home/bappaditya/projects/guessing_game/target
/debug/incremental -L
dependency=/home/bappaditya/projects/guessing_game/target/debug/deps --
extern rand=/home/bappaditya/projects/guessing_game/target/debug/deps/libra
nd-78fc4b142cc921d4.rlib`
error: Edition 2018 is unstable and only available for nightly builds of rustc.
我使用 rustup update
更新了 Rust,然后将 extern crate rand;
添加到我的 main.rs。现在它按预期工作。
程序 运行 但在我的 vscode 问题选项卡中它仍然显示错误 -
error[E0432]: unresolved import `rand`
--> src/main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing `extern crate rand;`?
快速修复是添加
edition = "2021"
到您的 Cargo.toml
,在 [dependencies]
行上方。
说明
Rust 主要版本 三个:Rust 2015、2018 和 2021。新代码建议使用 Rust 2021,但由于 Rust 需要向后兼容,因此您有选择使用它。
在 Rust 2015 中,您必须在使用 std
之外的任何内容之前编写 extern crate
语句。这就是错误消息的来源。但是在 Rust 2018 或 2021 中你不必再这样做了,这就是设置版本修复它的原因。
Rust 2021 有更多变化;如果您有兴趣,可以在 edition guide.
中阅读有关它们的信息