create a password with a custom length and uppercase letters in rust

Here's an example function that generates a password string with a custom length and using uppercase letters in Rust:

main.rs
use rand::Rng;

fn generate_password(length: usize) -> String {
    let mut rng = rand::thread_rng();
    let password: String = (0..length)
        .map(|_| rng.gen_range(b'A', b'Z' + 1) as char)
        .collect();
    password
}

fn main() {
    let password = generate_password(10);
    println!("Password: {}", password);
}
329 chars
15 lines

This code uses the Rust rand crate to generate random numbers and characters. The generate_password function takes a length parameter that specifies the length of the password to generate. It then generates a random string of uppercase letters with the same length as the parameter length.

To generate a password with length 10, you would call generate_password(10) and it would output a random string of 10 uppercase letters like AJKNSDRMPO.

gistlibby LogSnag