| Crates.io | substr-iterator |
| lib.rs | substr-iterator |
| version | 0.1.3 |
| created_at | 2025-01-11 16:30:47.602675+00 |
| updated_at | 2025-01-12 19:54:28.074574+00 |
| description | Substring extractor based on characters without allocation |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1512515 |
| size | 33,258 |
This library is made to iterate over a &str by a number of characters without allocating.
cargo add substr-iterator
use substr_iterator::{Trigram, TrigramIter};
let mut iter = TrigramIter::from("whatever");
assert_eq!(iter.next(), Some(['w', 'h', 'a']));
let mut iter = TrigramIter::from("今天我吃饭");
assert_eq!(iter.next(), Some(['今', '天', '我']));
It's also possible to handle bigger windows.
use substr_iterator::{Substr, SubstrIter};
let mut iter = SubstrIter::<2>::from("whatever");
assert_eq!(iter.next(), Some(['w', 'h']));
let mut iter = SubstrIter::<2>::from("今天我吃饭");
assert_eq!(iter.next(), Some(['今', '天']));
When the std feature is enabled, the SubstrWrapper allows to display Substr as a String.
use substr_iterator::{SubstrWrapper, Trigram, TrigramIter};
let mut iter = TrigramIter::from("whatever");
let item = SubstrWrapper(iter.next().unwrap());
assert_eq!(item.to_string(), "wha");
When the serde feature is enabled, the SubstrWrapper allows to serialize and deserialize.
use substr_iterator::{SubstrWrapper, Trigram, TrigramIter};
let data: Vec<SubstrWrapper<3>> = vec![
SubstrWrapper(['a', 'b', 'c']),
SubstrWrapper(['今', '天', '我']),
];
assert_eq!(
serde_json::to_string(&data).unwrap(),
"[\"abc\",\"今天我\"]",
);