如何在Rust中禁用「不必要的类型括号」警告?(模块/项目级)
Rust禁用「不必要的类型括号」警告的方法
a) 针对部分模块/代码块禁用
如果只想在特定模块、结构体或代码段中禁用该警告,可使用#[allow(unused_parens)]属性:
- 单个结构体禁用:
#[allow(unused_parens)] pub struct MyStruct { my_vec: Vec<(f64)> }
- 整个模块禁用:
#[allow(unused_parens)] mod target_module { pub struct MyStruct { my_vec: Vec<(f64)> } }
b) 针对整个项目禁用
有两种方式实现全局禁用:
- 在 crate 根文件(
main.rs或lib.rs)最顶部添加全局属性:
#![allow(unused_parens)] pub struct MyStruct { my_vec: Vec<(f64)> }
- 修改
Cargo.toml,为所有编译配置添加 rustflags:
[profile.dev] rustflags = ["-A", "unused_parens"] [profile.release] rustflags = ["-A", "unused_parens"]
内容的提问来源于stack exchange,提问作者Petras Purlys
相关产品推荐
相关产品推荐

