#macro #compile-time #skip

skipcode

A Rust macro library enabling compile-time skipping of statements/blocks

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

MIT license

10KB
101 lines

SkipCode

crates.io version crates.io downloads docs.rs license rustc version no_std

A zero‑cost macro library for compile‑time conditional code skipping
Works on no_std targets, 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_std compatible – 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


License

Dual‑licensed under the MIT license. See the LICENSE-MIT file in the repository.


Happy skipping! 🚀

No runtime deps