macro_rules! slice_sep {
($inner:expr) => { ... };
($inner:expr, $sep:expr) => { ... };
}
👎Deprecated since 0.8.0-rc.1: Use
string_v2
instead, v1 will be removed in 0.9.0.Expand description
Create a SliceSep
.
§Examples
Generally, this macro takes a slice of StringExtT
types, like Vec<T>
or &[T]
where T implements StringExtT
.
let slice_sep = slice_sep!(["a", "b", "c"], ',');
assert_eq!(
str_concat!("Hello: ", &slice_sep, " World!"),
"Hello: a,b,c World!"
);
let slice_sep = slice_sep!(vec!["a", "b", "c"], ',');
assert_eq!(
str_concat!(sep = ';'; "TEXT_1", slice_sep, "2_TEXT"),
"TEXT_1;a,b,c;2_TEXT"
);
Map is accepted too.
let slice_sep = slice_sep!(["a", "b", "c"].iter().map(|s| s.to_ascii_uppercase()), ',');
assert_eq!(
str_concat!("Hello: ", slice_sep, " World!"),
"Hello: A,B,C World!"
);
In fact all types that implements StringExtT
can be used, though useless
and do not do so.
let slice_sep = slice_sep!("a", ',');
assert_eq!(
str_concat!(sep = ';'; "TEXT_1", &slice_sep, "2_TEXT"),
"TEXT_1;a;2_TEXT"
);
§Notes
Take the following as an example:
ⓘ
let post_ids = vec![1, 2, 3];
let l = slice_sep!(
post_ids
.iter()
.map(|id| { slice_sep!(("post_ids[]=", id), "") }),
'&'
)
.to_string_ext();
let r = slice_sep!(post_ids.iter().map(|id| { ("post_ids[]=", id) }), '&').to_string_ext();
assert_eq!(l, r);
Since the contents in tuple like ("post_ids[]=", id)
are recognized as
independent, the separator will be also inserted between them.
To avoid this, you can use slice_sep!
with an empty separator to avoid
recognizing the contents as independent, while ()
is better:
let post_ids = vec![1, 2, 3];
let l = slice_sep!(
post_ids
.iter()
.map(|id| { slice_sep!(("post_ids[]=", id), "") }),
'&'
)
.to_string_ext();
let r = slice_sep!(
post_ids
.iter()
.map(|id| { slice_sep!(("post_ids[]=", id)) }), // No separator specified.
'&'
)
.to_string_ext();
assert_eq!(l, r);