create a password with numbers in rust

You can create a secure password with numbers in Rust by using the rand crate to generate random numbers and selecting a subset of those numbers as the password. Here is an example function that generates a random password consisting of 10 digits:

main.rs
use rand::Rng;

fn generate_password() -> String {
    let mut rng = rand::thread_rng();
    let password: String = (0..10)
        .map(|_| rng.gen_range(0, 10)) // generate a random digit
        .map(|n| char::from_digit(n, 10).unwrap()) // convert digit to char
        .collect();
    password
}
301 chars
11 lines

This code first creates a random number generator using thread_rng from the rand crate. It then generates 10 random digits, converts each digit to a char, and collects them into a String to create the password. You can customize the length of the password and the character set to use by adjusting the parameters passed to gen_range and from_digit.

gistlibby LogSnag