Crates.io | allwords |
lib.rs | allwords |
version | 0.1.2 |
source | src |
created_at | 2021-02-08 09:31:28.335664 |
updated_at | 2021-11-18 14:59:07.106399 |
description | Generate all the words over a given alphabet |
homepage | |
repository | https://github.com/lmammino/allwords |
max_upload_size | |
id | 352234 |
size | 23,728 |
allwords
is a Rust crate that allows you to generate words over a given alphabet.
Word generation can be useful in several scenarios:
Pseudo-random data generation (e.g. testing / mocking)
Brute forcing of keys / passwords
Id or Serial number generation
To install the library add the following lines to your Cargo.toml
[dependencies]
allwords = "0"
Or, if you have cargo add
, you can run the following command:
cargo add allwords@0
The basic idea for using this library is that you create an Alphabet
with a set of
characters and then you can use it to generate a WordsIterator
. You can use the iterator
to generate all the possible words over the alphabet.
For instance if you want to generate all the possible words containing "a"
, "b"
, "c"
with
a maximum length of 3 chars:
use allwords::{Alphabet};
let a = Alphabet::from_chars_in_str("abc").unwrap();
let words: Vec<String> = a.all_words(Some(3)).collect();
let expected_words: Vec<String> = [
"a", "b", "c",
"aa", "ab", "ac", "ba", "bb", "bc", "ca", "cb", "cc",
"aaa", "aab", "aac", "aba", "abb", "abc", "aca", "acb", "acc",
"baa", "bab", "bac", "bba", "bbb", "bbc", "bca", "bcb", "bcc",
"caa", "cab", "cac", "cba", "cbb", "cbc", "cca", "ccb", "ccc"]
.iter()
.map(|s| s.to_string())
.collect();
assert_eq!(words, expected_words);
Once you create an alphabet a
, there are 4 different ways to get an iterator:
a.all_words(max_len)
- Creates an iterator that will generate all the words for a given alphabet. You can optionally specifify a maximum length, after which, the iterator will terminate.a.all_words_unbound()
- A shortcut for creating an unbound (endless) iterator for the given alphabet.a.all_words_starting_from(start_word, max_len)
- Creates an iterator that will generate all the words for a given alphabet starting from a given word.a.all_words_with_len(start_len, max_len)
- Creates an iterator that will generate all the words for a given alphabet starting from the first word with a given minimum length.Consult the crate documentation for more details and examples.
Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.
Licensed under MIT License. © Luciano Mammino.