diff --git a/README.md b/README.md index a77ad61..9dd554a 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,29 @@ fn duration_as_days(dur: Duration) -> u64 { } ``` +Emitting Cargo cfg directives from a build script. Note that this requires +listing `rustversion` under `[build-dependencies]` in Cargo.toml, not +`[dependencies]`. + +```rust +// build.rs + +fn main() { + if rustversion::cfg!(since(1.36)) { + println!("cargo:rustc-cfg=no_std"); + } +} +``` + +```rust +// src/lib.rs + +#![cfg_attr(no_std, no_std)] + +#[cfg(no_std)] +extern crate alloc; +``` +
#### License diff --git a/src/lib.rs b/src/lib.rs index 7da533a..5352f93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,6 +150,29 @@ //! } //! ``` //! +//! Emitting Cargo cfg directives from a build script. Note that this requires +//! listing `rustversion` under `[build-dependencies]` in Cargo.toml, not +//! `[dependencies]`. +//! +//! ``` +//! // build.rs +//! +//! fn main() { +//! if rustversion::cfg!(since(1.36)) { +//! println!("cargo:rustc-cfg=no_std"); +//! } +//! } +//! ``` +//! +//! ``` +//! // src/lib.rs +//! +//! #![cfg_attr(no_std, no_std)] +//! +//! #[cfg(no_std)] +//! extern crate alloc; +//! ``` +//! //!
#![doc(html_root_url = "https://docs.rs/rustversion/1.0.18")]