struct SemanticDirection;

fn main() {}
warning: struct is never used: `SemanticDirection`
 --> src/main.rs:1:1
  |
1 | struct SemanticDirection;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

我将在任何严重的情况下重新打开这些警告,但我只是在修补语言,这让我很抓狂。

我尝试添加#[允许(dead_code)]到我的代码,但这并不管用。


当前回答

把这两行放在文件的顶部。

#![allow(dead_code)]
#![allow(unused_variables)]

其他回答

你可以:

在结构、模块、函数等上添加allow属性: #(允许(dead_code)) struct SemanticDirection; 添加一个板条箱级别的允许属性;注意!: (允许(dead_code)) # ! 将它传递给rustc: rustc -A dead_code main.rs 使用cargo通过RUSTFLAGS环境变量传递它: RUSTFLAGS="$RUSTFLAGS - dead_code"货物构建

使用特性

#[cfg(feature = "dead_code")]

注意:“dead_code”可以被任何单词替换。

把这两行放在文件的顶部。

#![allow(dead_code)]
#![allow(unused_variables)]

另外,rust还提供了四个级别的检测(允许、警告、拒绝和禁止)。

https://doc.rust-lang.org/rustc/lints/levels.html#lint-levels

直接的方法就是把下面的内容放在文件的头部 #![允许(dead_code unused_variables)] dead_code lint检测未使用的、未导出的项。 unused_variables lint检测没有以任何方式使用的变量。 更简单的方法是将以下内容放在文件头部 # !(允许(未使用的))

参考:锈棉列表