Global feature gates in Cargo

1.5k views Asked by At

I would like to enable a feature gate for my entire Cargo project. For example, I would like #![feature(non_ascii_idents)] added to every source file. Is there a place to list them in Cargo.toml?

2

There are 2 answers

2
DK. On BEST ANSWER

No, though you don't add feature gates to every source file; they are crate attributes. That is, you set them on the crate, not on every module.

1
Nicolas Marshall On

There are two types of attributes:

  • file attributes (starting with #). They apply to the whole file only.
  • crate attributes (starting with #!). They apply to the whole crate at once.

What you want (#![feature(non_ascii_idents)]) is a crate attribute, so you need to place it once at the top of the crate's main file. That main file is usually:

  • src/main.rs for binaries
  • src/lib.rs for libraries