Generate random strings, chars, booleans, and numbers.
If you want to create random texts, phone numbers, or passwords, you can use random_str to do so.
Visit the GitHub repository or the Codeberg mirror.
You need to have installed cargo on your system.
If you are using Windows, download the installer from the official page in the following link:
https://www.rust-lang.org/tools/installIf you are using macOS, GNU/Linux, WSL or a UNIX based system just type:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shCreate a project.
cargo new my_projectIn your Cargo.toml file, add the following dependency:
[dependencies]
random_str = "1.1.0"
Use cargo build to download the dependency.
cargo buildIn your main.rs file write the following:
use random_str::random::{self, CharBuilder, RandomCharBuilder, RandomStringBuilder};
fn main() {
// Random letter including from 'a' to 'z' and from 'A' to 'Z'
let random_letter: Option<char> = RandomCharBuilder::new()
.with_lowercase()
.with_uppercase()
.build();
println!("Random letter: {}", random_letter.unwrap());
let random_symbol: Option<char> = RandomCharBuilder::new()
.with_symbols()
.build();
println!("Random symbol: {}", random_symbol.unwrap());
let random_number = random::number(0..9);
println!("Random number: {}", random_number);
let another_random_number = random::number(1.0..2.0);
println!("Random float number: {}", another_random_number);
let digits: Option<String> = RandomStringBuilder::new()
.with_length(10)
.with_numbers()
.build();
let random_phone_number = format!("+52 {}", digits.unwrap());
println!("Random phone number: {}", random_phone_number);
let random_password: Option<String> = RandomStringBuilder::new()
.with_length(32) // Optional, 16 as default
.with_lowercase()
.with_uppercase()
.with_numbers()
.with_symbols()
.build();
println!("Random password: {}", random_password.unwrap());
let random_bool = random::bool();
println!("Random bool: {}", random_bool);
}Use cargo run to test the result.
cargo runPossible output:
Random letter: C
Random symbol: #
Random number: 5
Random float number: 1.487004718282926
Random phone number: +52 11932115293
Random password: M29Fd*tamf3spJxxZOVZRUil!vBjycUHD
Random bool: trueIf you think there is a function that may be helpful, you can open a pull request. Please, write your commits using Conventional Commits.
This project is licensed under the MIT license. See the LICENSE file for more information.
Made with