cfg-if

An if/elif-like macro for chaining Rust #[cfg] conditional-compilation branches

Library
Cargo
v1.0.4
642stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
47/100Fair
Development Activity28
Maintenance24
Community56
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
72/100Good
Architecture65
Code Quality75
Innovation60
Learning Curve90

cfg-if is a small, dependency-free Rust crate maintained under the rust-lang organization that provides a single cfg_if! macro for ergonomically expressing a chain of #[cfg(...)] conditional-compilation branches. Instead of duplicating an item under separate #[cfg]/#[cfg(not(...))] attributes for every platform or feature combination, cfg_if! lets you write an if/else-if/else chain and have the macro expand it into the correct mutually-exclusive #[cfg] attributes.

It is one of the most widely depended-on crates in the Rust ecosystem (over a billion downloads), used throughout the standard library’s own dependency graph and by low-level crates like libc, winapi-adjacent crates, and cross-platform runtime crates that need per-target or per-feature code paths.

What You Get

  • The cfg_if! macro supporting arbitrarily long if/else-if/else chains of #[cfg(...)] predicates
  • Automatic expansion into mutually exclusive #[cfg]/#[cfg(not(any(...)))] attributes so branches never conflict
  • Zero required runtime dependencies — the crate compiles to essentially nothing at runtime, it is a compile-time-only macro
  • An optional rustc-dep-of-std feature enabling the crate to be used inside the Rust standard library’s own build
  • no_std compatibility by design, since the crate has no runtime component at all

Common Use Cases

  • Low-level systems crates selecting a different implementation per target OS (Unix vs Windows vs WASM) without duplicating function signatures under multiple #[cfg] attributes
  • Crates with optional Cargo features that need to swap an implementation in/out (e.g. a std vs no_std code path)
  • Cross-platform FFI or syscall wrapper crates picking the correct binding set for the current target_arch/target_os combination
  • Any crate maintaining several feature-gated code paths that would otherwise require repetitive #[cfg(not(any(…)))] boilerplate

Under The Hood

Architecture: The entire crate is a single src/lib.rs file (212 lines) containing one macro_rules! definition for cfg_if!. The macro recursively parses an if/else-if/else token sequence and, for each branch, accumulates the negations of all preceding predicates so the emitted #[cfg] attributes are provably non-overlapping — this recursive-descent-over-tokens approach is the entire implementation. Tech Stack: Pure Rust 2018 edition, MSRV 1.32, with a single optional dependency (rustc-std-workspace-core, gated behind the rustc-dep-of-std feature used only when the crate is vendored into the Rust standard library’s own build graph) — no other runtime or build dependencies exist. Code Quality: Because the crate is a single declarative macro with no runtime logic, correctness is validated through the macro’s own doctested examples and downstream usage across the ecosystem (a billion-plus downloads act as an implicit integration test suite); the macro_rules! pattern matching is dense but the file is small enough to audit in full in minutes. API Design: The public surface is exactly one macro invoked with familiar if/else-if/else syntax mirroring ordinary Rust control flow, so there is effectively zero learning curve for anyone who already knows #[cfg] attributes — this minimalism is the crate’s entire value proposition.

Used by 7 apps in this directory

Dart
74%
AGPL 3.0

AppFlowy

Productivity · Project Management · Collaboration

75,729

The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.

View details
66
Repo Health
81
Technical
66
Dependency
Built with
Dart74%
Rust24%
Updated 1 weeks ago
Go
32%
Apache 2.0

CubeSandbox

Developer Tools · Security · AI Agents

11,247

Instant, concurrent, hardware-isolated MicroVM sandboxes for AI agents — E2B-API compatible, sub-60ms cold starts, and a built-in zero-trust egress proxy, all self-hostable at scale.

View details
83
Repo Health
88
Technical
63
Dependency
Built with
Go32%
Rust31%
C21%
Updated today
Rust
69%
Other

GitButler

Developer Tools · Devops · AI Development

21,531

Git, but better — a modern version control client with stacked branches, parallel workflows, unlimited undo, and first-class support for AI-powered development.

View details
89
Repo Health
84
Technical
68
Dependency
Built with
Rust69%
TypeScript18%
Svelte12%
Updated today
Go
62%
MIT

hoop

Security · Monitoring

791

A wire-protocol gateway that enforces data masking, command blocking, approval workflows, and full session recording for engineers and AI agents accessing production infrastructure.

View details
86
Repo Health
80
Technical
66
Dependency
Built with
Go62%
Clojure20%
JavaScript10%
Updated yesterday
TypeScript
95%
MIT

melty

Developer Tools · AI Code Assistants · Code Editors

5,449

The AI code editor where every chat message is a git commit you can revert, branch, or squash

View details
35
Repo Health
75
Technical
68
Dependency
Built with
TypeScript95%
Updated 1 years ago
Rust
68%
AGPL 3.0

RustDesk

Networking

121,233

Open-source, self-hosted remote desktop built in Rust — your data, your infrastructure, no third-party cloud.

View details
93
Repo Health
84
Technical
75
Dependency
Built with
Rust68%
Dart24%
Updated today
TypeScript
95%
Apache 2.0

Void

AI Code Assistants · Code Editors · Automation

28,833

Open-source AI code editor with direct LLM integration and data privacy

View details
49
Repo Health
84
Technical
69
Dependency
Built with
TypeScript95%
Updated 2 months ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search