Trait rayon::str::ParallelString

source ·
pub trait ParallelString {
Show 13 methods // Required method fn as_parallel_string(&self) -> &str; // Provided methods fn par_chars(&self) -> Chars<'_> { ... } fn par_char_indices(&self) -> CharIndices<'_> { ... } fn par_bytes(&self) -> Bytes<'_> { ... } fn par_encode_utf16(&self) -> EncodeUtf16<'_> { ... } fn par_split<P: Pattern>(&self, separator: P) -> Split<'_, P> { ... } fn par_split_inclusive<P: Pattern>( &self, separator: P ) -> SplitInclusive<'_, P> { ... } fn par_split_terminator<P: Pattern>( &self, terminator: P ) -> SplitTerminator<'_, P> { ... } fn par_lines(&self) -> Lines<'_> { ... } fn par_split_whitespace(&self) -> SplitWhitespace<'_> { ... } fn par_split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_> { ... } fn par_matches<P: Pattern>(&self, pattern: P) -> Matches<'_, P> { ... } fn par_match_indices<P: Pattern>(&self, pattern: P) -> MatchIndices<'_, P> { ... }
}
Expand description

Parallel extensions for strings.

Required Methods§

source

fn as_parallel_string(&self) -> &str

Returns a plain string slice, which is used to implement the rest of the parallel methods.

Provided Methods§

source

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);
source

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);
source

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, consider string.as_bytes().par_iter().copied() instead.

Examples
use rayon::prelude::*;
let max = "hello".par_bytes().max();
assert_eq!(Some(b'o'), max);
source

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);
source

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 Pattern trait is private, for use only by Rayon itself. It is implemented for char, &[char], [char; N], &[char; N], and any function or closure F: 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);
source

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 Pattern trait is private, for use only by Rayon itself. It is implemented for char, &[char], [char; N], &[char; N], and any function or closure F: 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."]);
source

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 to par_split, except it doesn’t produce an empty substring after a trailing terminator.

Note: the Pattern trait is private, for use only by Rayon itself. It is implemented for char, &[char], [char; N], &[char; N], and any function or closure F: 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);
source

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\n or 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);
source

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 Property White_Space. If you only want to split on ASCII whitespace instead, use 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);
source

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_Space instead, use 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);
source

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 Pattern trait is private, for use only by Rayon itself. It is implemented for char, &[char], [char; N], &[char; N], and any function or closure F: 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);
source

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 Pattern trait is private, for use only by Rayon itself. It is implemented for char, &[char], [char; N], &[char; N], and any function or closure F: 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")]);

Implementations on Foreign Types§

source§

impl ParallelString for str

Implementors§