Simon Warta
Simon Warta

Reputation: 11428

Enable Rust nightly feature when project feature is enabled

In a library crate I want to make backtraces available on demand and use the Rust nightly backtrace feature. In order to do that, Rust requires setting #![feature(backtrace)] in my crate root.

Is there a way to express I want Rust nightly feature "backtrace" only when my create level feature "backtraces" is set?

Non compiling pseudo code to help illustrating what I have in mind:

#[cfg(feature = "backtraces")]
#![feature(backtrace)]

Upvotes: 4

Views: 1617

Answers (1)

Ross MacArthur
Ross MacArthur

Reputation: 5459

You can use cfg_attr:

#![cfg_attr(feature = "backtraces", feature(backtrace))]

If the first argument is true then the subsequent attribute(s) will be applied.

Upvotes: 5

Related Questions