0

Is it possible to read the currently used Rust edition in a build script? Maybe through an environment variable?

I tried EDITION and CARGO_EDITION, but it didn't work. I didn't find any documentation on this either.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69

1 Answers1

4

Is it possible to read the currently used Rust edition in a build script?

I'm going to ignore your direct question because there isn't a single edition in play and the edition shouldn't matter.

Every crate in the entire dependency graph chooses which edition it will be compiled with. If your crate is written to use the 2018 edition, just your crate will be compiled using the edition settings. Then, the MIR of all the crates, a version-independent implementation, is stuck together to produce the final result.

If you are writing a library, opting into the 2018 edition is no different than opting into some feature that is only available in a newer version of Rust. Since the 2018 edition went stable in Rust 1.31, that would be the equivalent version.


One situation that I can see needing to know the edition is if you are generating code. In that case, I'd advocate for one of:

  • only generating code that works in both editions
  • your code accepts an argument from the end user that specifies what kind of code to generate.
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366