我今天开始学习Rust,但是我陷于this step。我想在项目中使用兰特板条箱,因此我按照教程中的建议更新了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 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;`?
答案 0 :(得分:4)
快速解决方案是添加
edition = "2018"
转到Cargo.toml
行上方的[dependencies]
。
Rust有两个主要的版本:Rust 2015和Rust2018。建议将Rust 2018用于新代码,但是由于Rust需要向后兼容,因此您必须选择使用它。
在Rust 2015中,您必须先编写extern crate
语句,然后才能使用std
之外的任何内容。这就是错误消息的来源。但是您不必再在Rust 2018中做到这一点,这就是为什么设置版本可以对其进行修复的原因。
Rust 2018还有更多更改;如果您有兴趣,可以在edition guide中阅读有关它们的信息。