macro_rules! split {
($s: expr, $pat: expr) => { ... };
}
Expand description
Returns an array of substrings of a string slice, separated by characters matched by a pattern.
The pattern type must be one of
This macro is const-context only.
See also str::split
.
ยงExamples
const SEPARATOR: &str = ", ";
const TEXT: &str = "lion, tiger, leopard";
const ANIMALS_ARRAY: [&str;3] = const_str::split!(TEXT, SEPARATOR);
const ANIMALS_SLICE: &[&str] = &const_str::split!(TEXT, SEPARATOR);
assert_eq!(ANIMALS_ARRAY, ANIMALS_SLICE);
assert_eq!(ANIMALS_SLICE, &["lion", "tiger", "leopard"]);
Split by any character in a slice:
const TEXT: &str = "one:two;three,four";
const SEPARATORS: &[char] = &[':', ';', ','];
const WORDS: &[&str] = &const_str::split!(TEXT, SEPARATORS);
assert_eq!(WORDS, &["one", "two", "three", "four"]);