AdwaitDash
AdwaitDash

Reputation: 143

Failed to select a version for the requirement `rand = "^0.9.0"`

I am getting this error every time while running 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 looks like this:

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

Upvotes: 14

Views: 11839

Answers (4)

C.I.D.C.
C.I.D.C.

Reputation: 1

I'm just a beginner learning Rust, but from what I've seen, if you use cargo search rand you get this output:

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>`

As you can see, the "0.9.0" is a beta version, which is why it gives you the error. If you still want to use it, you can write rand = { version = "0.9.0-beta.1" } in the Cargo.toml file.

If you just want the latest stable version of rand (or any other dependency, from what I've seen up until now), you can use cargo add rand which will automatically add the "0.8.5" version to the Cargo.toml file.

If, for some reason, you want to add them manually, you can use cargo info rand, which will give you this output:

username@PC:~/Projects/guessing_game$ cargo info rand
rand #random #rng
Random number generators and other randomness functionality.
version: 0.8.5 (latest 0.9.0-beta.1)
license: MIT OR Apache-2.0
rust-version: unknown
documentation: https://docs.rs/rand
homepage: https://rust-random.github.io/book
repository: https://github.com/rust-random/rand
crates.io: https://crates.io/crates/rand/0.8.5
features:
 +default       = [std, std_rng]
  alloc         = [rand_core/alloc]
  getrandom     = [rand_core/getrandom]
  libc          = [dep:libc]
  rand_chacha   = [dep:rand_chacha]
  std           = [rand_core/std, rand_chacha/std, alloc, getrandom, libc]
  std_rng       = [rand_chacha]
  log           = [dep:log]
  min_const_gen = []
  nightly       = []
  packed_simd   = [dep:packed_simd]
  serde         = [dep:serde]
  serde1        = [serde, rand_core/serde1]
  simd_support  = [packed_simd]
  small_rng     = []
note: to see how you depend on rand, run `cargo tree --invert --package [email protected]`

Then you can add the version you see in the output to the Cargo.toml file.

Upvotes: 0

Calmarius
Calmarius

Reputation: 19431

Another possible reason for getting an error like this is that your compiler is outdated and the new version of the library targets the new compiler. Update your toolchain with rustup update.

I have a project that used clap 4.2, but which refused to compile on another computer with an error like this.

Upvotes: 6

EpicAdidash
EpicAdidash

Reputation: 159

This error is caused because there is no version 0.9.0 available. Update it to 0.8.0. Cargo.toml should look like this.

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

Upvotes: 11

Nishanth
Nishanth

Reputation: 81

I experienced this myself, I believe the confusion is stemming from the tutorial on Rust mentioning 0.9.0 as an example to understand upgrading Rust crates.

I'm not sure why they chose that example, since it doesn't exist. Would be a better idea to do 0.7.x being upgraded to 0.8.0 if you're new to Rust and want to play around with upgrading Rust crates.

Upvotes: 7

Related Questions