So I'm totally new to Rust world, and I was trying to make a program that generates random numbers from 0 to 255
Seems so simple! so what I did is this
extern crate rand;
use rand::Rng;
fn main() {
println!("Guess the number!");
let random_number: u8 = rand::thread_rng().gen_range(0, 255);
println!("Your random number is {}", random_number);
}
Now this works fine but the problem with this approach is that number 255 will not be included because according to http://ift.tt/1PYpMyt
The gen_range method takes two numbers as arguments and generates a random number between them. It’s inclusive on the lower bound but exclusive on the upper bound
When i try to do this
let random_number: u8 = rand::thread_rng().gen_range(0, 256);
Rust will generate a warning! because we all know that u8 accepts values from 0 to 255.
My question is how to work around this without needing to change random_number data type?
Aucun commentaire:
Enregistrer un commentaire