bon_macros

Macro set

Source
set!() { /* proc-macro */ }
Expand description

Creates any set-like collection that implements FromIterator<T>.

It automatically converts each value to the target type using Into. This way you can write a set of Strings without the need to call .to_owned() or .to_string() on every string literal:

let set: HashSet<String> = bon::set![
    "value1",
    format!("value{}", 2),
    "value3",
];

There is no separate variant for BTreeSet and HashSet. Instead, you should annotate the return type of this macro with the desired type or make sure the compiler can infer the collection type from other context.

§Compile errors

The macro conservatively rejects duplicate values in the set with a compile error. This check works for very simple expressions that involve only literal values.

let set: HashSet<String> = bon::set![
    "value1",
    "value2"
    "value1", // compile error: `duplicate value in the set`
];