MMateus Felipe/hideoutv10
  • About
  • Writing
  • Projects
  • Books
GitHub
All postsNote·Dec 11, 2023

01Note

Random Strings in Rust

How to generate a random fixed size string in Rust

Mateus Felipe Gonçalves

@mateusfg7

rustrandstringgenerate
Random Strings in Rust

For generate a random string with fixed size in Rust, we can use Alphanumeric trait from rand crate:

use rand::{distributions::Alphanumeric, Rng}; // 0.8
 
fn main() {
  let s: String = rand::thread_rng()
    .sample_iter(&Alphanumeric)
    .take(7)
    .map(char::from)
    .collect();
  println!("{}", s);
}
 

ref: https://stackoverflow.com/questions/54275459/how-do-i-create-a-random-string-by-sampling-from-alphanumeric-characters

02Comments

Join the conversation.

Questions, corrections or a different take? Sign in with GitHub or Google and leave a note below.

Comments need a login

Loading comments...

Thread

00 comments

Session

Not signed in. Sign in with GitHub or Google to comment, reply and react.

03Keep reading

Related posts.

Note·Dec 13, 2023

Rust Box

Rust Box
rustbox

Note·Feb 07, 2024

`iter` vs `into_iter`

`iter` vs `into_iter`
rustiterators

Note·Feb 05, 2024

Subslice Pattern Rust

Subslice Pattern Rust
rustpattern
MMateus Felipe

Full stack by day, root user by night. Notes, projects and experiments from my corner of the internet.

contato@mateusf.comWhatsApp +55 37 99995-7218

01Pages

  • Home
  • About
  • Writing
  • Projects
  • Books
  • Sitemap

02Elsewhere

  • GitHub
  • LinkedIn
  • Reddit
  • Instagram
© 2026 Mateus Felipe Gonçalvesv9.mateusf.com