Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global feature gates in Cargo

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?

like image 732
yong Avatar asked Jun 14 '15 06:06

yong


People also ask

What is feature gates?

A feature gate is a high level tool to turn features on and off. It provides metadata about features, a simple, opinionated API, and avoid many potential pitfalls of other systems, such as using preferences directly.

What is features in cargo TOML?

Cargo "features" provide a mechanism to express conditional compilation and optional dependencies. A package defines a set of named features in the [features] table of Cargo. toml , and each feature can either be enabled or disabled.


2 Answers

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.

like image 135
DK. Avatar answered Oct 19 '22 06:10

DK.


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
like image 32
Nicolas Marshall Avatar answered Oct 19 '22 06:10

Nicolas Marshall