我每次运行时都会收到此错误
cargo build
:
error: failed to select a version for the requirement `rand = "^0.9.0"`
candidate versions found which didn't match: 0.8.5, 0.8.4, 0.8.3, ...
location searched: crates.io index
required by package `guessing_game v0.1.0 (D:\Adwait\Rust\project\guessing_game)`
Cargo.toml
看起来像这样:
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.9.0"
此错误是因为没有可用的版本
0.9.0
而导致的。将其更新为0.8.0
。 Cargo.toml 应该看起来像这样。
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.0"
我自己也经历过这一点,我相信这种困惑源于Rust 教程提到 0.9.0 作为示例来了解升级 Rust 板条箱。
我不确定他们为什么选择这个例子,因为它不存在。如果您是 Rust 新手并且想要尝试升级 Rust crate,那么将 0.7.x 升级到 0.8.0 是一个更好的主意。
出现此类错误的另一个可能原因是您的编译器已过时,并且新版本的库针对新编译器。使用
rustup update
更新您的工具链。
我有一个使用 clap 4.2 的项目,但它拒绝在另一台计算机上编译,并出现这样的错误。
我只是学习 Rust 的初学者,但从我所见,如果你使用
cargo search rand
你会得到这个输出:
username@PC:~/Projects/guessing_game$ cargo search rand
rand = "0.9.0-beta.1" # Random number generators and other randomness functionality.
bevy_rand = "0.8.0" # A plugin to integrate rand for ECS optimised RNG for the Bevy game engine.
tinyrand = "0.5.0" # Lightweight RNG specification and several ultrafast implementations in Rust.
rand-construct = "0.10.0" # Encapsulates the random-constructible and random-constructible-derive crates which are used for cr…
random_derive = "0.0.0" # Procedurally defined macro for automatically deriving rand::Rand for structs and enums
tera-rand = "0.2.0" # A suite of random data generation functions for the Tera template engine
tera-rand-cli = "0.2.0" # A CLI tool for generating a feed of random data from a Tera template
rands = "0.8.8" # Random number generators and other randomness functionality.
faker_rand = "0.1.1" # Fake data generators for lorem ipsum, names, emails, and more
rand_derive2 = "0.1.21" # Generate customizable random types with the rand crate
... and 1581 crates more (use --limit N to see more)
note: to learn more about a package, run `cargo info <name>`
如您所见,“0.9.0”是测试版本,这就是它给您错误的原因。如果你还想使用它,可以在 Cargo.toml 文件中写入
rand = { version = "0.9.0-beta.1" }
。
如果您只想要 rand 的最新稳定版本(或任何其他依赖项,从我到目前为止所看到的来看),您可以使用
cargo add rand
它将自动将“0.8.5”版本添加到 Cargo.toml文件。