At the moment, when writing a Rust crate which optionally supports the standard library you need to add cargo feature (typically called
std) and wrap the code requiring the standard library in
#[cfg(feature = "std")].
Furthermore, if you have dependencies which themselves may also require the standard library, you need to manually determine which feature flag they use and edit your
Cargo.toml to enable to that feature whenever your
std is enabled.
Would people be open to adding a new
std configuration option (i.e.
#[cfg(std)]) which is automatically enabled whenever any crate within the dependency tree is compiled without
The use cases I have in mind are:
- being able to automatically include
std::error::Errorimpls or implement a trait on items from the standard library
- making sure a crate is never used with the standard library (e.g.
#[cfg(std)] compile_error!("This crate will not work outside an embedded context")) so you get a user-friendly compile error instead of the more confusing "the
stdcomponent for this target isn't installed"
- Providing a single blessed convention for creating
no_stdcrates which doesn't require manually propagating feature flags all the time
- Lower the barrier to entry for writing crates which work without the standard library by default
There is already a similar issue by @susurrus from a couple years ago, but I'm wondering if opinions have changed since then.