在Rust中解决导入

7cwmlq89  于 2023-10-20  发布在  其他
关注(0)|答案(4)|浏览(201)

我在从crates.io导入兰德crate时遇到问题。在添加行兰德=“0.8.3”之后,然后为项目运行命令cargo build,它仍然显示相同的错误:

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

error[E0433]: failed to resolve: use of undeclared crate or module `rand`
 --> main.rs:4:25
  |
4 |     let secret_number = rand::thread_rng().gen_range(1..=11);
  |                         ^^^^ use of undeclared crate or module `rand`

error: aborting due to 2 previous errors

cargo.toml文件

[package]
name = "roller"
version = "0.1.0"
authors = ["User"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.3"

基本上,最简单的可复制示例是这一行代码:

use rand::Rng;

 fn main(){
    let secret_number = rand::thread_rng().gen_range(1..=11);
    print!("{}",secret_number);
 }

有什么问题吗?
以防万一:

cargo.lock文件:

# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
 "cfg-if",
 "libc",
 "wasi",
]

[[package]]
name = "libc"
version = "0.2.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"

[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"

[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
 "libc",
 "rand_chacha",
 "rand_core",
 "rand_hc",
]

[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
 "ppv-lite86",
 "rand_core",
]

[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
 "getrandom",
]

[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
 "rand_core",
]

[[package]]
name = "roller"
version = "0.1.0"
dependencies = [
 "rand",
]

[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
8gsdolmq

8gsdolmq1#

在文件Cargo.toml中添加行:

[dependencies]
rand="0.3.14"

重建项目!
它适用于这些版本:

  • rustc 1.53.0
  • 货物1.53.0

从这里获取答案https://github.com/rust-lang/rust/issues/34616

qvtsj1bj

qvtsj1bj2#

当我用vs code(rust-analyzer)运行它时,也得到了同样的错误。
当你按下运行按钮rustic main.rs这个命令被调用在终端,它会导致错误.
在终端输入这个,

cargo build
cargo run

它工作得很好

zlwx9yxi

zlwx9yxi3#

如果你认为你做的一切都是正确的,并得到一个奇怪的无法导入错误,那么你可以尝试cargo clean
这将允许你在那之后完全重新构建你的二进制文件/库。

vs3odd8k

vs3odd8k4#

我对生 rust 还是个新手。我在学习Rust Lang的教程时也遇到了同样的问题。我已经将rand = "0.8.5"行添加到cargo.toml文件中,但我不明白为什么它仍然无法编译。
问题是我在顶部添加了这一行。它需要在dependencies部分:

[dependencies]
rand = "0.8.5"

相关问题