Rust – Generate Random Names

How to generate Random names in rust

In this tutorial we will see how to generate random names in Rust using various awesome crates.

(a). Use names

names is a Random name generator for Rust.

It provides a generate that constructs random name strings suitable for use in container instances, project names, application instances, etc.

The name Generator implements the Iterator trait so it can be used with adapters, consumers, and in loops.

Step 1: Install it

To install names declare it as a dependency in your Cargo.toml:

names = { version = "0.12.0", default-features = false }

Step 2: Write Code

Here are some examples:

Example 1: Return a Name

You can use the default Generator to return a name:

use names::Generator;

let mut generator = Generator::default();
println!("Your project is: {}",;
// #=> "Your project is: rusty-nail"

To generate more randomness, use a name with a trailing 4-digit number:

use names::{Generator, Name};

let mut generator = Generator::with_naming(Name::Numbered);
println!("Your project is: {}",;
// #=> "Your project is: pushy-pencil-5602"

Example 2: with Custom Generators

You can supply your own adjectives and nouns by providing 2 string slices:

use names::{Generator, Name};

let adjectives = &["imaginary"];
let nouns = &["roll"];
let mut generator = Generator::new(adjectives, nouns, Name::default());



Read more here.

(b). Use random_name_generator

It is a library that Generates random names in various languages.

It generates it's results based upon which language file is specified. Currently, the following are supported:

  • Elven
  • Fantasy
  • Goblin
  • Roman

Step 1: Install it

To install it, add the following to your Cargo.toml file:

random_name_generator = "0.1.2"

Step 2: Write Code

Here is an example:

use rnglib::{RNG, Language};

fn main() {
    let rng = RNG::new(&Language::Elven).unwrap();

    let first_name = rng.generate_name();
    let last_name = rng.generate_name();

    println!("{}: {} {}",, first_name, last_name)

You can control the number of syllables for the generated name as follows:

use rnglib::{RNG, Language};

fn main() {
    let rng = RNG::new(&Language::Fantasy).unwrap();
    let name = rng.generate_name_by_count(3);
    println!("{}: {}",, name)

Furthermore you can pass in a custom language file:

use rnglib::{RNG};

fn main() {
    let rng = RNG::new_from_file("src/languages/Test-tiny.txt").unwrap();
    let name = rng.generate_name();
    println!("{}: {}",, name)


Read more here or here.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *