Trait ParallelString
pub trait ParallelString
Parallel extensions for strings.
Required Methods
fn as_parallel_string(&self) -> &strReturns a plain string slice, which is used to implement the rest of the parallel methods.
Provided Methods
fn par_chars(&self) -> Chars<'_>Returns a parallel iterator over the characters of a string.
Examples
use rayon::prelude::*; let max = "hello".par_chars().max_by_key(|c| *c as i32); assert_eq!(Some('o'), max);fn par_char_indices(&self) -> CharIndices<'_>Returns a parallel iterator over the characters of a string, with their positions.
Examples
use rayon::prelude::*; let min = "hello".par_char_indices().min_by_key(|&(_i, c)| c as i32); assert_eq!(Some((1, 'e')), min);fn par_bytes(&self) -> Bytes<'_>Returns a parallel iterator over the bytes of a string.
Note that multi-byte sequences (for code points greater than
U+007F) are produced as separate items, but will not be split across threads. If you would prefer an indexed iterator without that guarantee, considerstring.as_bytes().par_iter().copied()instead.Examples
use rayon::prelude::*; let max = "hello".par_bytes().max(); assert_eq!(Some(b'o'), max);fn par_encode_utf16(&self) -> EncodeUtf16<'_>Returns a parallel iterator over a string encoded as UTF-16.
Note that surrogate pairs (for code points greater than
U+FFFF) are produced as separate items, but will not be split across threads.Examples
use rayon::prelude::*; let max = "hello".par_encode_utf16().max(); assert_eq!(Some(b'o' as u16), max); let text = "Zażółć gęślą jaźń"; let utf8_len = text.len(); let utf16_len = text.par_encode_utf16().count(); assert!(utf16_len <= utf8_len);fn par_split<P: Pattern>(&self, separator: P) -> Split<'_, P>Returns a parallel iterator over substrings separated by a given character or predicate, similar to
str::split.Note: the
Patterntrait is private, for use only by Rayon itself. It is implemented forchar,&[char],[char; N],&[char; N], and any function or closureF: Fn(char) -> bool + Sync + Send.Examples
use rayon::prelude::*; let total = "1, 2, buckle, 3, 4, door" .par_split(',') .filter_map(|s| s.trim().parse::<i32>().ok()) .sum(); assert_eq!(10, total);fn par_split_inclusive<P: Pattern>(&self, separator: P) -> SplitInclusive<'_, P>Returns a parallel iterator over substrings separated by a given character or predicate, keeping the matched part as a terminator of the substring similar to
str::split_inclusive.Note: the
Patterntrait is private, for use only by Rayon itself. It is implemented forchar,&[char],[char; N],&[char; N], and any function or closureF: Fn(char) -> bool + Sync + Send.Examples
use rayon::prelude::*; let lines: Vec<_> = "Mary had a little lamb\nlittle lamb\nlittle lamb." .par_split_inclusive('\n') .collect(); assert_eq!(lines, ["Mary had a little lamb\n", "little lamb\n", "little lamb."]);fn par_split_terminator<P: Pattern>(&self, terminator: P) -> SplitTerminator<'_, P>Returns a parallel iterator over substrings terminated by a given character or predicate, similar to
str::split_terminator. It's equivalent topar_split, except it doesn't produce an empty substring after a trailing terminator.Note: the
Patterntrait is private, for use only by Rayon itself. It is implemented forchar,&[char],[char; N],&[char; N], and any function or closureF: Fn(char) -> bool + Sync + Send.Examples
use rayon::prelude::*; let parts: Vec<_> = "((1 + 3) * 2)" .par_split_terminator(|c| c == '(' || c == ')') .collect(); assert_eq!(vec!["", "", "1 + 3", " * 2"], parts);fn par_lines(&self) -> Lines<'_>Returns a parallel iterator over the lines of a string, ending with an optional carriage return and with a newline (
\r\nor just\n). The final line ending is optional, and line endings are not included in the output strings.Examples
use rayon::prelude::*; let lengths: Vec<_> = "hello world\nfizbuzz" .par_lines() .map(|l| l.len()) .collect(); assert_eq!(vec![11, 7], lengths);fn par_split_whitespace(&self) -> SplitWhitespace<'_>Returns a parallel iterator over the sub-slices of a string that are separated by any amount of whitespace.
As with
str::split_whitespace, 'whitespace' is defined according to the terms of the Unicode Derived Core PropertyWhite_Space. If you only want to split on ASCII whitespace instead, use [par_split_ascii_whitespace]ParallelString::par_split_ascii_whitespace.Examples
use rayon::prelude::*; let longest = "which is the longest word?" .par_split_whitespace() .max_by_key(|word| word.len()); assert_eq!(Some("longest"), longest);All kinds of whitespace are considered:
use rayon::prelude::*; let words: Vec<&str> = " Mary had\ta\u{2009}little \n\t lamb" .par_split_whitespace() .collect(); assert_eq!(words, ["Mary", "had", "a", "little", "lamb"]);If the string is empty or all whitespace, the iterator yields no string slices:
use rayon::prelude::*; assert_eq!("".par_split_whitespace().count(), 0); assert_eq!(" ".par_split_whitespace().count(), 0);fn par_split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_>Returns a parallel iterator over the sub-slices of a string that are separated by any amount of ASCII whitespace.
To split by Unicode
White_Spaceinstead, use [par_split_whitespace]ParallelString::par_split_whitespace.Examples
use rayon::prelude::*; let longest = "which is the longest word?" .par_split_ascii_whitespace() .max_by_key(|word| word.len()); assert_eq!(Some("longest"), longest);All kinds of ASCII whitespace are considered, but not Unicode
White_Space:use rayon::prelude::*; let words: Vec<&str> = " Mary had\ta\u{2009}little \n\t lamb" .par_split_ascii_whitespace() .collect(); assert_eq!(words, ["Mary", "had", "a\u{2009}little", "lamb"]);If the string is empty or all ASCII whitespace, the iterator yields no string slices:
use rayon::prelude::*; assert_eq!("".par_split_whitespace().count(), 0); assert_eq!(" ".par_split_whitespace().count(), 0);fn par_matches<P: Pattern>(&self, pattern: P) -> Matches<'_, P>Returns a parallel iterator over substrings that match a given character or predicate, similar to
str::matches.Note: the
Patterntrait is private, for use only by Rayon itself. It is implemented forchar,&[char],[char; N],&[char; N], and any function or closureF: Fn(char) -> bool + Sync + Send.Examples
use rayon::prelude::*; let total = "1, 2, buckle, 3, 4, door" .par_matches(char::is_numeric) .map(|s| s.parse::<i32>().expect("digit")) .sum(); assert_eq!(10, total);fn par_match_indices<P: Pattern>(&self, pattern: P) -> MatchIndices<'_, P>Returns a parallel iterator over substrings that match a given character or predicate, with their positions, similar to
str::match_indices.Note: the
Patterntrait is private, for use only by Rayon itself. It is implemented forchar,&[char],[char; N],&[char; N], and any function or closureF: Fn(char) -> bool + Sync + Send.Examples
use rayon::prelude::*; let digits: Vec<_> = "1, 2, buckle, 3, 4, door" .par_match_indices(char::is_numeric) .collect(); assert_eq!(digits, vec![(0, "1"), (3, "2"), (14, "3"), (17, "4")]);
Implementors
impl ParallelString for str