This library performs brace expansion of strings, similar in spirit (though different in several details) to that used in shells like Bash etc.
Given the input:
{hello,goodbye,wonderful} world
this algorithm produces the following collection of strings:
hello world
goodbye world
wonderful world
Note that unlike shell brace expansion, the result is a collection of separate strings rather than a single string. Also, whitespace characters are not treated specially by the algorithm; they are on the same footing as printing characters.
Curly braces {
and }
are used to mark the start and end of an expansion
list, and commas separate the items in each list. Literal curly braces and
commas must be escaped with single backslashes:
this is {\{braced\},[bracketed\, nicely]}
produces:
this is {braced}
this is [bracketed, nicely]
Note that in Rust source we must escape the backslashes within ordinary string literals an additional time, giving the following:
let output = brace_expand("this is {\\{braced\\},[bracketed\\, nicely]}");
assert_eq!(output, vec!["this is {braced}", "this is [bracketed, nicely]"]);
If you want a literal backslash, that must be escaped by writing it as a double backslash:
this is a backslash: \\
produces:
this is a backslash: \
In Rust source, this would look as follows:
let output = brace_expand("this is a backslash: \\\\");
println!("{}", output[0]);
which produces the following output:
this is a backslash: \
The following code snippet illustrates how inputs can contain multiple lists, and even be nested:
let output = brace_expand("{hello,goodbye} {world,my {friends,colleagues}}");
assert_eq!(
output,
vec![
"hello world",
"hello my friends",
"hello my colleagues",
"goodbye world",
"goodbye my friends",
"goodbye my colleagues",
]
);
brace-expand
's current minimum supported Rust version (MSRV) is 1.31.1.
brace-expand
is guaranteed to compile with that version. It might also
compile with older versions, but that could change in a future patch release.
If the MSRV of brace-expand
changes, that will be done in a minor version
release (e.g. 1.0.x -> 1.1.0).