120

In order to get a feel for how Rust works, I decided to look at a little terminal-based text editor called Iota. I cloned the repository and ran cargo build only to be told:

error: *if let* syntax is experimental

help: add #![feature(if_let)] to the crate attributes to enable

Where am I supposed to add #![feature(if_let)] to the crate attributes?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Soham Chowdhury
  • 2,125
  • 2
  • 18
  • 29
  • 3
    As a side-note, newer (nightly) builds of Rust have enabled this feature by default, so updating Rust will also remove your problem. – Shepmaster Dec 13 '14 at 03:16

1 Answers1

131

A crate attribute is an attribute (#[...]) that applies to the enclosing context (#![...]). This attribute must be added to the top of your crate root, thus the context is the crate itself:

#![attribute_name]
#![attribute_name(arg1, ...)]

If you are creating

  • a library — the crate root will be a file called lib.rs
  • an application — the crate root would be the primary .rs file you build. In many cases, this will be called main.rs
  • an integration test - the crate root is each file in tests/
  • an example - the crate root is each file in examples/

The Rust Programming Language and the Rust Reference talk a bit about attributes in general. The Unstable Book contains a list of feature flags and brief documentation on what they do.

There are many different crate attributes, but the feature crate attribute (#![feature(feature1, feature2)]) may only be used in a nightly version of the compiler. Unstable features are not allowed to be used in stable Rust versions.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Why #![...] is called Inner attributes? in the substrate project, there is a line on top of file `#![cfg_attr(not(feature = "std"), no_std)]` what is this line means? does it have some relation to the declaration in the Cargo.toml file in which usually a definition like `[features] default = ['std']` – gfan Nov 26 '21 at 07:18
  • @gfan because it's an attribute _inside_ of the thing it's applied to. See also https://docs.rust-embedded.org/book/intro/no-std.html – Shepmaster Nov 26 '21 at 15:00