Expand description
This module provides two interfaces for accessing clusters from an underlying string. The
GraphemeCluster
trait extends the Peekable
iterators over Chars
or CharIndices
to add a next_cluster
method which returns Option<String>
with the next
cluster if one exists. This is the best method for getting individual clusters from a stream which is normally
only getting char
s but is not recommended if you wish to iterate over clusters.
let mut char_iterator = "A\u{301}โ๐ฝ๐ฆ๐น!".chars().peekable();
assert_eq!(char_iterator.next_cluster(), Some("A\u{301}".to_string()));
assert_eq!(char_iterator.next_cluster(), Some("โ๐ฝ".to_string()));
assert_eq!(char_iterator.next_cluster(), Some("๐ฆ๐น".to_string()));
assert_eq!(char_iterator.next_cluster(), Some("!".to_string()));
assert_eq!(char_iterator.next_cluster(), None);
For the iterating over clusters case there is a struct Graphemes
which implements iterator
and can be constructed from a &str
. This returns references to substrings of the original
&str
and is more performant for that case than the extended iterator provided through
GraphemeCluster
which allocates a new String
for each cluster found.
let graphemes = Graphemes::new("A\u{301}โ๐ฝ๐ฆ๐น!");
assert_eq!(graphemes.collect::<Vec<&str>>(), ["A\u{301}", "โ๐ฝ", "๐ฆ๐น", "!"])
Structsยง
- Graphemes
Graphemes
provides an iterator over the grapheme clusters of a string.
Traitsยง
- Grapheme
Cluster - Get the next grapheme cluster from a stream of characters or char indices
This trait is implemented for any
Peekable
iterator over eitherchar
or(usize, char)
(so it will work onPeekable<Chars>
andPeekable<CharIndices>
as well as any other peekable iterator which meets this requirement. - Peek
Char - This trait exists primarily to allow a single implementation to be used for both
Peekable<Chars>
andPeekable<CharIndices>
. You could implement this for some other iterator if you like as long as you can implement the two methods below.