如何将 rustc 标志传递给货物? [英] How to pass rustc flags to cargo?

查看:40
本文介绍了如何将 rustc 标志传递给货物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试禁用死代码警告.我尝试了以下

cargo build -- A dead_code

<块引用>

➜ rla git:(master) ✗ cargo build --A dead_code错误:无效参数.

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

解决方案

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

<小时>

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

#![deny(some_lint)]//拒绝此模块及其子模块中的 lint#[allow(another_lint)]//在这个函数中允许 lintfn foo() {...}

另见:

I am trying to disable dead code warnings. I tried the following

cargo build -- -A dead_code

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

So I am wondering how would I pass rustc arguments to cargo?

解决方案

You can pass flags through Cargo by several different means:

  • cargo rustc, which only affects your crate and not its dependencies.
  • The RUSTFLAGS environment variable, which affects dependencies as well.
  • Some flags have a proper Cargo option, e.g., -C lto and -C panic=abort can be specified in the Cargo.toml file.
  • Add flags in .cargo/config using one of the rustflags= keys.

However, in your specific case of configuring lints, you don't need to use compiler flags; you can also enable and disable lints directly in the source code using attributes. This may in fact be a better option as it's more robust, more targeted, and doesn't require you to alter your build system setup:

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

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

See also:

这篇关于如何将 rustc 标志传递给货物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆