rust - 如何将 rustc 标志传递给 cargo ?

标签 rust rust-cargo

我正在尝试禁用无效代码警告。我尝试了以下

cargo build -- -A dead_code

➜ rla git:(master) ✗ cargo build -- -A dead_code error: Invalid arguments.

所以我想知道如何将 rustc 参数传递给 cargo?

最佳答案

您可以通过几种不同的方式通过 Cargo 传递标志:

  • cargo rustc,它只会影响您的包而不影响其依赖项。
  • RUSTFLAGS环境变量,它也会影响依赖关系。
  • 一些标志具有适当的 Cargo 选项,例如,-C lto-C panic=abort 可以在 Cargo.toml 中指定> 文件。
  • .cargo/config中添加标志使用 rustflags= 键之一。

但是,在配置 lints 的特定情况下,您不需要使用编译器标志;您还可以使用属性直接在源代码中启用和禁用 lints。这实际上可能是一个更好的选择,因为它更健壮、更有针对性,并且不需要您更改构建系统设置:

#![deny(some_lint)] // deny lint in this module and its children

#[allow(another_lint)] // allow lint in this function
fn foo() {
    ...
}

另见:

关于rust - 如何将 rustc 标志传递给 cargo ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38040327/

相关文章:

multithreading - 我应该如何为并行计算生成线程?

Rust Diesel 无法编译并出现链接器错误

rust - Vec::iter() 转换为借用 Option

rust - 功能 'xxx' 依赖于 'yyy',这不是可选依赖项

docker - 编译 azure iot edge 的 edgelet 模块时出现 cargo 错误

rust - 匹配: Deref and mark operator, 为什么它们不一样?

struct - 是否可以在 Rust 中声明本地匿名结构?

rust - 如何为库指定macOS部署目标?

unit-testing - 有没有某种方法可以构建包含测试文档字符串的 Rust 文档?

rust - 无法加载依赖 list