Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)]
like image 234
Simon Warta Avatar asked Oct 18 '25 14:10

Simon Warta


1 Answers

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.

like image 168
Ross MacArthur Avatar answered Oct 22 '25 06:10

Ross MacArthur