Skip to content

Commit

Permalink
Add build.rs example
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 26, 2024
1 parent a1f0c09 commit 54a2749
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```

<br>

#### License
Expand Down
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
//! ```
//!
//! <br>
#![doc(html_root_url = "https://docs.rs/rustversion/1.0.18")]
Expand Down

0 comments on commit 54a2749

Please sign in to comment.