macro_rules! str_split {
($string:expr, $splitter:expr $(,)?) => { ... };
}
Expand description
Splits $string
(a &'static str
constant) with $splitter
,
returning an array of &'static str
s.
§Alternatives
For an alternative macro which will be usable in slice patterns
(once inline_const_pat
is stabilized)
you can use str_split_pat
.
§Signature
This macro acts like a function of this signature:
fn str_split(string: &'static str, splitter: impl Splitter) -> [&'static str; LEN]
and is evaluated at compile-time.
impl Splitter
is any of these types:
-
&'static str
-
char
-
u8
: only ascii values (0 up to 127 inclusive) are allowed
The value of LEN
depends on the string
and splitter
arguments.
§Example
use const_format::str_split;
assert_eq!(str_split!("this is nice", ' '), ["this", "is", "nice"]);
assert_eq!(str_split!("Hello, world!", ", "), ["Hello", "world!"]);
// A `""` splitter outputs all chars individually (`str::split` does the same)
assert_eq!(str_split!("🧡BAR🧠", ""), ["", "🧡", "B", "A", "R", "🧠", ""]);
// Splitting the string with an ascii byte
assert_eq!(str_split!("dash-separated-string", b'-'), ["dash", "separated", "string"]);
{
const STR: &str = "foo bar baz";
const SPLITTER: &str = " ";
// both arguments to the `str_aplit` macro can be non-literal constants
const SPLIT: [&str; 3] = str_split!(STR, SPLITTER);
assert_eq!(SPLIT, ["foo", "bar", "baz"]);
}