create a list of 5 random numbers in rust

To generate a list of 5 random numbers in Rust, you will need the rand crate. First, add rand to your dependencies in Cargo.toml:

[dependencies]
rand = "0.8.4"
30 chars
3 lines

Then, you can use the gen_range method of the Rng trait to generate each random number, and add it to a vector to store the list of numbers:

main.rs
use rand::{Rng, thread_rng};

fn main() {
    let mut rng = thread_rng();
    let mut random_numbers = Vec::new();

    for _ in 0..5 {
        let random_number = rng.gen_range(1, 101);
        random_numbers.push(random_number);
    }

    println!("{:?}", random_numbers);
}
278 chars
14 lines

This code creates a new thread_rng and initializes an empty vector to hold the random numbers. Then, it loops 5 times, generating a random number between 1 and 100 using gen_range and adding it to the vector. Finally, it prints out the entire vector of random numbers.

gistlibby LogSnag