3 releases
Uses new Rust 2024
| new 0.1.2 | Jan 31, 2026 |
|---|---|
| 0.1.1 | Jan 22, 2026 |
| 0.1.0 | Jan 20, 2026 |
#1479 in Rust patterns
10KB
101 lines
SkipCode
A zero‑cost macro library for compile‑time conditional code skipping
Works onno_stdtargets, supports both debug and release builds, and provides a few ergonomic helper macros.
Why SkipCode?
- Zero runtime overhead – all skipping is performed during compilation (
if const). no_stdcompatible – the crate has no dependencies and can be used on bare‑metal platforms.- Type‑safe – skipped code is still parsed and type‑checked, preventing silent mismatches.
- Works in both Debug and Release – the same macros behave identically regardless of the build profile.
- Fine‑grained control – you can skip individual statements or blocks inside a function, something
#[cfg]cannot do.
If you need to conditionally compile items (modules, functions, impl blocks) keep using #[cfg].
If you need to conditionally compile statements or expressions, reach for skipcode.
Features
| Feature | Description |
|---|---|
| Zero‑cost | Skipped code is removed completely at compile time; no runtime checks. |
| Debug/Release compatible | Works the same in both build configurations. |
no_std ready |
#![cfg_attr(not(test), no_std)] – usable on embedded targets. |
| Type‑safe | Skipped code still participates in Rust’s type system. |
| Flexible syntax | Accepts blocks (skip! { … }), single statements (skip!(x = 1)), and empty blocks. |
| Conditional skipping | skip_if!(cond, …) – skip when cond (a compile‑time bool) is true. |
| Negated conditional | skip_unless!(cond, …) – run only when cond is true. |
| Debug‑only helper | skip_debug! { … } – shorthand for skip_unless!(cfg!(debug_assertions), …). |
| Clear compile‑time errors | Detects empty input, missing code after a comma, and empty statements, emitting helpful compile_error! messages. |
Installation
Add the dependency with Cargo:
[dependencies]
skipcode = "0.2"
or via the CLI:
cargo add skipcode
The crate works out‑of‑the‑box; no feature flags are required.
Quick Start
use skipcode::{skip, skip_if, skip_unless, skip_debug};
fn main() {
// 1️⃣ Unconditional skipping – the block disappears completely.
skip! {
// This never runs, but the compiler still type‑checks it.
let _unused: u32 = "not a number".parse().unwrap();
}
// 2️⃣ Conditional skipping – true ⇒ skip, false ⇒ keep.
skip_if!(cfg!(debug_assertions), {
// Debug‑only validation.
assert!(std::env::var("USER").is_ok());
});
// 3️⃣ Negated condition – runs only when the condition is true.
let mut counter = 0;
skip_unless!(cfg!(target_os = "windows"), counter += 1);
// 4️⃣ Debug‑only shortcut.
skip_debug! {
println!("Only printed in debug builds");
}
}
All macros expand to a single expression (() when skipped), so they can be placed anywhere a statement is allowed.
Documentation & Resources
- API reference – https://docs.rs/skipcode
- Repository – https://github.com/RustFactory/skipcode
- Issue tracker – use the repo’s Issues tab for bugs or feature requests.
License
Dual‑licensed under the MIT license. See the LICENSE-MIT file in the repository.
Happy skipping! 🚀