let secret_num = rand::thread_rng().gen_range(1, 101);
But In the official documentation, the code is
let secret_num = rand::thread_rng().gen_range(1..=100);

Why is it returning an range instead of an integer.
Full Code
use std::io;
use std::cmp::Ordering;
use rand::Rng;
use colored::*;
fn main() {
    let secret_num = rand::thread_rng().gen_range(1..=100);
    println!("The secret number is {}", secret_num);
    println!("Guess the Number!");
    loop {
        println!("Please input you guess: ");
        let mut guess = String::new();
        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");
            
        println!("You guessed: {}", guess);
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };
        
        match guess.cmp(&secret_num) {
            Ordering::Less => println!("{}", "Too small!".red()),
            Ordering::Greater => println!("{}", "Too big!".red()),
            Ordering::Equal => {
                println!("{}", "You win!".green());
                break;
            },
        }
    }
}
                You're looking at documentation for [email protected] while you're using some older version.
The API differs in that the former takes a Range
fn gen_range<T, R>(&mut self, range: R) -> T
where
    T: SampleUniform,
    R: SampleRange<T>,
{
    //...
}
but the older version you're using expects a low and high bound.
fn gen_range<T: SampleUniform, B1, B2>(&mut self, low: B1, high: B2) -> T
where
    B1: SampleBorrow<T> + Sized,
    B2: SampleBorrow<T> + Sized,
{
    //...
}
In your case rust-analyzer or whatever you use infers T to be a Range because the first argument low is of type B1: SampleBorrow<T> + Sized, (something that can be borrowed and returns a reference to T aka Range here). If you read the further error it should also tell you that it's still missing a second parameter high.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With