Environment $RUSTFLAGS override things unexpectly

I once set the $RUSTFLAGS to a crazy state, to achieve the fastest possible performance of Rust

-C opt-level=3 -C target-cpu=native -C codegen-units=1 -C lto

And then, my compile time blows up dramastically :innocent:, it takes about 20 seconds to compile the syn crate.

I've searched for some solutions, such as "using mold" , but that is just a no-op. To figure out what happened, I tested a lot and finally figure that, when enable mold, the command it suggests is:

rustflags = ["-C", "link-arg=-fuse-ld=/usr/bin/mold"]

That flag is rustflags, which is replaced by the defined environment variable (even without a warning).

I had fixed the issue, remove the RUSTFLAGS environment, using a custom script calls rustc and config cargo with config.toml, but I still wonder whether I forgot something:

  1. What is the usage of environment variable RUSTFLAGS? Should it be set automatically?
  2. By what design cargo decided not to merge RUSTFLAGS with the custom one, but make RUSTFLAGS rules the current one?
  3. Should the be a warning or a message in the future?

The design for accumulating flags is still open for debate.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.