3 releases
0.1.2 | Nov 18, 2021 |
---|---|
0.1.1 | Feb 8, 2021 |
0.1.0 | Feb 8, 2021 |
#864 in Encoding
Used in biogarden
22KB
257 lines
allwords 🦀
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
Install
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
Sample Usage
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);
WordsIterator
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.
Contributing
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.
License
Licensed under MIT License. © Luciano Mammino.