create a password with lowercase letters in rust

In Rust, you can generate a password with only lowercase letters using the rand crate. Here's how you can generate a random password consisting of lowercase letters:

main.rs
use rand::Rng;

fn generate_password(len: usize) -> String {
    let mut rng = rand::thread_rng();
    let chars: Vec<char> = (0..len)
        .map(|_| rng.gen_range(b'a', b'z' + 1) as char)
        .collect();
    chars.iter().collect()
}

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

In this code, the generate_password function generates a len-length password containing only lowercase letters, and returns it as a String. The rng.gen_range method generates a random number between the ASCII code of a and z, and the (n..m) syntax creates an iterator over the range. The collect method collects the characters into a vector, and the iter and collect methods are then used to convert the vector into a String. Finally, the main function generates a 10-character password and prints it to the console.

gistlibby LogSnag