Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to generate random string without repetition in Rust

1 Answer

0 votes
use rand::seq::SliceRandom; // choose
use std::collections::HashSet;

fn generate_unique_random_string(total: usize) -> String {
    let chars: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".chars().collect();
    let mut used_chars = HashSet::new();
    let mut result = String::new();
    let mut rng = rand::thread_rng();

    while result.len() < total {
        let &random_char = chars.choose(&mut rng).unwrap();
        if used_chars.insert(random_char) {
            result.push(random_char);
        }
    }

    result
}

fn main() {
    println!("{}", generate_unique_random_string(15));
}




/*
run:
  
eThP9niWV65x8aU
  
*/

 



answered Nov 4, 2024 by avibootz

Related questions

...