gpt4 book ai didi

rust - 变量不需要是可变的,但它确实是可变的

转载 作者:行者123 更新时间:2023-12-03 11:25:21 25 4
gpt4 key购买 nike

我有一个可以工作的宏:

#[macro_export]
macro_rules! btreemap {
($( $key: expr => $val: expr ),*) => {{
let mut map = ::std::collections::BTreeMap::new();
$( map.insert($key, $val); )*
map
}}
}
但编译器警告:
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)
如果我删除 mut ,我收到一个错误:
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)
即如果 mut它警告空调用,但如果不是非空调用错误。
我怎样才能解决这个问题?

最佳答案

How can I fix this?


您可以通过添加 #[allow(unused_mut)] 来抑制警告。到宏生成的代码:
macro_rules! btreemap {
($( $key: expr => $val: expr ),*) => {{
#[allow(unused_mut)]
let mut map = ::std::collections::BTreeMap::new();
$( map.insert($key, $val); )*
map
}}
}
警告与警告所能获得的一样良性,这种警告抑制经常出现在宏定义中。
另一种可能性是你已经发现的,特殊情况下的空扩展。但我更喜欢显式警告抑制,因为它消除了(虚假)警告而不会使宏复杂化。您介绍的特殊情况实际上并不是必需的 - 没有它会生成完全相同的代码。

关于rust - 变量不需要是可变的,但它确实是可变的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63643732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com