Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

rust - How to pass rustc flags to cargo?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...