I have a macro which works:
#[macro_export]
macro_rules! btreemap {
($( $key: expr => $val: expr ),*) => {{
let mut map = ::std::collections::BTreeMap::new();
$( map.insert($key, $val); )*
map
}}
}
but which the compiler warns about:
warning: variable does not need to be mutable
--> src/util.rs:16:11
|
16 | let mut map = ::std::collections::BTreeMap::new();
| ----^^^
| |
| help: remove this `mut`
|
::: src/foo.rs:49:13
|
79 | foo: btreemap!{},
| ----------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
If I remove the mut, I get an error:
error[E0596]: cannot borrow `map` as mutable, as it is not declared as mutable
--> src/util.rs:17:10
|
16 | let map = ::std::collections::BTreeMap::new();
| --- help: consider changing this to be mutable: `mut map`
17 | $( map.insert($key, $val); )*
| ^^^ cannot borrow as mutable
|
::: src/bar.rs:110:18
|
116 | let bar = btreemap!{quux => wibble};
| -------------------------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
i.e. If mut it warns on empty invocations, but if it's not if errors on non-empty invocations.
How can I fix this?