在 Cargo 项目中使用 crate 出现“可能缺少 extern crate"错误; [英] Using a crate in a Cargo project errors with "maybe a missing extern crate"

查看:62
本文介绍了在 Cargo 项目中使用 crate 出现“可能缺少 extern crate"错误;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天开始学习 Rust,但我被困在 这一步.我想在我的项目中使用 rand crate,所以我按照教程中的建议更新了我的 Cargo.toml:

I started learning Rust today, but I am stuck at this step. I want use the rand crate in my project, so I updated my Cargo.toml as suggested in the tutorial:

[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;

它给出了这个错误:

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

我错过了什么吗?

我按照建议添加了 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 --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.现在它按预期工作.


I updated rust using rustup update and then added extern crate rand; to my main.rs. Now it's working as expected.

程序运行,但在我的 vscode 问题选项卡中仍然显示错误 -

The program runs but in my vscode problems tab its still showing the error -

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

推荐答案

快速解决方法是添加

edition = "2018"

到您的 Cargo.toml,在 [dependencies] 行上方.

to your Cargo.toml, above the [dependencies] line.

Rust 有两个主要的版本:Rust 2015 和 Rust 2018.建议新代码使用 Rust 2018,但由于 Rust 需要向后兼容,您必须选择使用它.

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 2015 中,您必须在使用 std 之外的任何内容之前编写一个 extern crate 语句.这就是错误消息的来源.但是在 Rust 2018 中您不必再这样做了,这就是设置版本修复它的原因.

In Rust 2015, you had to write an extern crate statement before using anything outside of std. 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; if you're interested, you can read about them in the edition guide.

这篇关于在 Cargo 项目中使用 crate 出现“可能缺少 extern crate"错误;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆