简体   繁体   English

在Cargo项目中使用板条箱时出现错误,提示“也许缺少外部板条箱”

[英]Using a crate in a Cargo project errors with “maybe a missing extern crate”

I started learning Rust today, but I am stuck at this step . 我今天开始学习Rust,但是在这一步上我陷于困境。 I want use the rand crate in my project, so I updated my Cargo.toml as suggested in the tutorial: 我想在项目中使用兰特板条箱,因此我按照教程中的建议更新了Cargo.toml

[package]
name = "guessing_game"
version = "0.1.0"
authors = ["Novice <novice.coder@gmail.com>"]

[dependencies]
rand = "0.3.14"

Importing it in my code as: 在我的代码中将其导入为:

use rand::Rng;

It gives this error: 它给出了这个错误:

error[E0432]: unresolved import `rand`
 --> src/main.rs:1:5
  |
1 | use rand::Rng;
  |     ^^^^ maybe a missing `extern crate rand;`?

Am I missing something? 我想念什么吗?


I added edition = "2018" as suggested: 我根据建议添加了edition = "2018"

Cargo.toml: 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 now gives: 货运量现在可以提供:

$ 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.

I updated rust using rustup update and then added extern crate rand; 我使用rustup update更新了rust,然后添加了extern crate rand; to my main.rs. 给我的main.rs。 Now it's working as expected. 现在它正在按预期工作。

The program runs but in my vscode problems tab its still showing the error - 该程序运行,但是在我的vscode问题选项卡中,它仍然显示错误-

error[E0432]: unresolved import `rand`
 --> src/main.rs:1:5
  |
1 | use rand::Rng;
  |     ^^^^ maybe a missing `extern crate rand;`?

The quick fix is to add 快速的解决方法是添加

edition = "2018"

to your Cargo.toml , above the [dependencies] line. [dependencies]行上方的Cargo.toml

Explanation 说明

There are two major editions of Rust: Rust 2015 and Rust 2018. Rust 2018 is recommended for new code, but since Rust needs to be backward compatible, you have to opt in to use it. Rust有两个主要版本 :Rust 2015和Rust2018。建议将Rust 2018用于新代码,但是由于Rust需要向后兼容,因此您必须选择使用它。

In Rust 2015, you had to write an extern crate statement before using anything outside of std . 在Rust 2015中,在使用std之外的任何东西之前,您必须编写一个extern crate语句。 That's where the error message comes from. 这就是错误消息的来源。 But you don't have to do that in Rust 2018 anymore, which is why setting the edition fixes it. 但是您不必再在Rust 2018中执行此操作,这就是为什么设置版本可以对其进行修复的原因。

There are many more changes in Rust 2018; Rust 2018还有很​​多变化; if you're interested, you can read about them in the edition guide . 如果您有兴趣,可以在版本指南中阅读有关它们的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM