skip to content

In Rust, a crate builds on nightly with `#![feature(iter_intersperse)]`; what does stable rustc report, and how can that nightly path stay optional?

level: middleimportance: should knowfreq 35%

answer

  1. a crate attribute, not a flag
  2. two different E-codes
  3. dependencies are crates too
  4. cfg_attr behind a Cargo feature

basics

~20 s

Stable rustc rejects any #![feature] attribute with E0554, in your crate or in a dependency. To keep nightly optional, enable the gate through cfg_attr behind an opt-in Cargo feature and keep a stable code path for everyone else.

solid answer

~40 s

A stable or beta compiler refuses every feature gate with E0554, "`#![feature]` may not be used on the stable release channel", even for a gate whose feature has since stabilised. Removing the attribute but still calling `Iterator::intersperse` gives E0658, "use of unstable library feature `iter_intersperse`", on any channel. Because the gate is crate-level, a dependency that has one fails to compile on stable and takes the whole build with it. The usual pattern is an opt-in Cargo feature such as `nightly`, a crate-root `#![cfg_attr(feature = "nightly", feature(iter_intersperse))]`, and `#[cfg]` code paths with a stable fallback. The feature stays out of `default`, and a nightly CI job tests it.

code

rust · 21 lines
rust
#![cfg_attr(feature = "nightly", feature(iter_intersperse))]

/// Joins words with ", ".
pub fn join(words: &[&str]) -> String {
    #[cfg(feature = "nightly")]
    {
        words.iter().copied().intersperse(", ").collect()
    }
    #[cfg(not(feature = "nightly"))]
    {
        words.join(", ")
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn joins() {
        assert_eq!(super::join(&["a", "b", "c"]), "a, b, c");
    }
}

go deeper

for a junior

Recall that unstable features need both a nightly compiler and a #![feature] attribute, and that stable rejects the attribute with E0554.

for a middle

Tell E0554 from E0658, explain why one gated dependency breaks a stable build, and write the cfg_attr pattern with a stable fallback.

for a senior

Judge when an opt-in nightly path is worth carrying: who tests it, how a dated nightly keeps it reproducible, and when stable_features says to delete it.

for a principal

Treat any mandatory nightly dependency as a policy question for every downstream user, and set a rule that shipped code builds on stable.

## What a feature gate is New Rust language features and standard-library APIs land on the main branch **behind a feature gate**: a named switch, such as `iter_intersperse` for `Iterator::intersperse`, that keeps the feature unusable unless a crate opts in. The opt-in is a **crate-level attribute** at the top of `lib.rs` or `main.rs`: ```rust #![feature(iter_intersperse)] ``` The Unstable Book documents each gate and links its tracking issue; for `iter_intersperse` that is issue #79524, and rustc's own error message carries the same link. Gated features are still being designed, so their names, signatures and behaviour may change between two nightly builds, or the feature may be removed. Reading the tracking issue tells you how close a feature is to stabilisation and what is still being argued about, which is the first thing to check before building on it. ## What each compiler reports Two different errors appear, and they mean different things: | Situation | Stable 1.99.1 | Nightly | |---|---|---| | `#![feature(iter_intersperse)]` present | **E0554**: "`#![feature]` may not be used on the stable release channel" | accepted | | no attribute, but `.intersperse(...)` called | **E0658**: "use of unstable library feature `iter_intersperse`" | E0658, plus help: "add `#![feature(iter_intersperse)]` to the crate attributes to enable" | | attribute names a gate that has since stabilised | still E0554, with help "remove the attribute" | a `stable_features` warning saying the feature "has been stable since" a given release | E0554 is about the **channel**: stable and beta refuse the attribute itself, whatever it names. E0658 is about the **feature**: the code uses something gated without opting in. Seeing E0658 on stable means the API is simply not available yet, and no attribute will make it so. ## Why one gated dependency breaks the whole build Every crate in a Cargo build is compiled by the same toolchain. If a dependency's `lib.rs` starts with an unconditional `#![feature(...)]`, a stable build stops at that crate with E0554 and Cargo reports "could not compile" for it. Cargo's habit of capping the **lints** of registry dependencies does not help: E0554 is an error, not a lint. In practice: - a crate that gates features unconditionally makes **every user** choose nightly; - the way out is to replace the dependency, patch it so the gate is optional, or move the whole build to nightly; - library authors therefore make any nightly-only path **opt-in**. ## Keeping the nightly path optional The standard shape combines a Cargo feature with conditional compilation, both covered in their own lessons: 1. Declare an empty Cargo feature, conventionally `nightly = []`, and keep it **out of `default`**. 2. At the crate root, write `#![cfg_attr(feature = "nightly", feature(iter_intersperse))]`, so the gate exists only when that Cargo feature is on. 3. Give each gated use a `#[cfg(feature = "nightly")]` body and a `#[cfg(not(feature = "nightly"))]` fallback that works on stable. 4. In CI, test the default build on stable and the `nightly` feature on a nightly toolchain, ideally a pinned dated nightly so the job does not break overnight. A user who turns the Cargo feature on while building on stable still gets E0554; the feature only moves the choice to the person who asked for it. Two warnings come with this pattern: - the Cargo feature is not a promise of stability: if the unstable API changes on nightly, that path breaks, and the crate should say so in its documentation; - once the gate stabilises, newer nightly compilers warn with `stable_features`, which is the signal to delete the gate and the fallback when the crate's minimum supported Rust version allows it. ## Choosing whether to use nightly at all The decision differs by what the crate is: - **An application** may depend on nightly if the feature is worth it, but that means pinning a dated nightly such as `nightly-2026-10-25`, moving to newer ones deliberately, and accepting that an upgrade can break the unstable code. - **A library** should almost never require a gate, because a mandatory gate forces every downstream user onto nightly; the opt-in pattern above is the acceptable form. - **Tooling and experiments**, such as running Miri or trying a proposed API, are where nightly earns its keep without touching what ships. Most teams therefore keep their shipped code on stable, and treat each gate they do carry as a dated debt with a tracking issue to watch.

  • Why does stable Rust reject `#![feature(let_chains)]` even though let chains are stable on the 2024 edition?
    A stable compiler refuses every feature gate with E0554, whatever it names; for a stabilised feature it adds the help "remove the attribute". A nightly compiler instead warns with `stable_features`. Let chains need no gate since Rust 1.88, so the fix is to delete the line.
  • A Rust build on stable fails with E0658 on a standard-library method and no `#![feature]` in sight; what does that mean?
    The method exists in std but is still unstable, so stable cannot use it at all; the error names the gate and links the tracking issue. The options are a stable alternative, a polyfill written in the crate, waiting for stabilisation, or an opt-in nightly path behind a Cargo feature.

  • A dependency's #![feature] is ignored on stable because Cargo caps dependency lints.
  • E0658 on stable can be fixed by adding the matching #![feature] attribute.
  • Enabling a Cargo feature named nightly switches Cargo to the nightly toolchain.
  • Once a feature stabilises, stable accepts its old #![feature] line harmlessly.
  • Putting the nightly feature in default is fine because stable users can disable it.