Search code examples
rustrust-cargo

Can't find the " rand " dependency


Can't find the " rand " dependency.

But cargo not working. He finds " rand " library, but it does not install " rand " library.

I do not know what to do. I thought that I had installed everything I needed. Maybe it's worth reinstalling rust ?

Maybe I didn't install all the packages for rust to work correctly?

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.3.14"

main.rs

use std::io;
use rand::Rng;

fn main() {
 println!("Угадайте число!");

 let secret_number = rand::thread_rng().gen_range(1, 101);

 println!("Пожалуйста, введите свою догадку.");

 let mut guess = String::new();

 io::stdin().read_line(&mut guess).expect("Не получилось прочитать строку");

 println!("Вы загадали: {}", guess);
}

@possum thanks

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.5"

Now he writes me this

C:\Dima\Rust\guessing_game>cargo run
   Compiling guessing_game v0.1.0 (C:\Dima\Rust\guessing_game)
error[E0277]: the trait bound `{integer}: SampleRange<_>` is not satisfied
   --> src\main.rs:7:51
    |
7   |  let secret_number = rand::thread_rng().gen_range(1, 101);
    |                                         --------- ^ the trait `SampleRange<_>` is not implemented for `{integer}`
    |                                         |
    |                                         required by a bound introduced by this call
    |
    = help: the following other types implement trait `SampleRange<T>`:
              std::ops::Range<T>
              RangeInclusive<T>
note: required by a bound in `gen_range`
   --> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rand-0.8.5\src\rng.rs:132:12
    |
129 |     fn gen_range<T, R>(&mut self, range: R) -> T
    |        --------- required by a bound in this associated function
...
132 |         R: SampleRange<T>
    |            ^^^^^^^^^^^^^^ required by this bound in `Rng::gen_range`

error[E0061]: this method takes 1 argument but 2 arguments were supplied
   --> src\main.rs:7:41
    |
7   |  let secret_number = rand::thread_rng().gen_range(1, 101);
    |                                         ^^^^^^^^^  -----
    |                                                    | |
    |                                                    | unexpected argument of type `{integer}`
    |                                                    help: remove the extra argument
    |
note: method defined here
   --> C:\Users\comp\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rand-0.8.5\src\rng.rs:129:8
    |
129 |     fn gen_range<T, R>(&mut self, range: R) -> T
    |        ^^^^^^^^^

Some errors have detailed explanations: E0061, E0277.
For more information about an error, try `rustc --explain E0061`.
error: could not compile `guessing_game` (bin "guessing_game") due to 2 previous errors

C:\Dima\Rust\guessing_game>

Solution

  • @possum and @Jmb thanks

    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.5"
    

    main.rs

    use std::io;
    use rand::Rng;
    
    fn main() {
      println!("Угадайте число!");
    
     let secret_number = rand::thread_rng().gen_range (1..101);
    
     println!("Пожалуйста, введите свою догадку.");
    
     let mut guess = String::new();
    
      io::stdin().read_line(&mut guess).expect("Не получилось прочитать строку");
    
      println!("Вы загадали: {}", guess);
    }
    

    conclusion

    C:\Dima\Rust\guessing_game>cargo run
    warning: unused variable: `secret_number`
     --> src\main.rs:7:6
      |
    7 |  let secret_number = rand::thread_rng().gen_range (1..101);
      |      ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_secret_number`
      |
      = note: `#[warn(unused_variables)]` on by default
    
    warning: `guessing_game` (bin "guessing_game") generated 1 warning (run `cargo fix --bin "guessing_game"` to apply 1 suggestion)
        Finished dev [unoptimized + debuginfo] target(s) in 0.03s
         Running `target\debug\guessing_game.exe`
    Угадайте число!
    Пожалуйста, введите свою догадку.
    32
    Вы загадали: 32