cfg-if

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

Library
Cargo
v1.0.5
646 stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
56 /100 Fair
Development Activity 56
Maintenance 32
Community 56
Maturity 60
Momentum 20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
72 /100 Good
Architecture 65
Code Quality 75
Innovation 60
Learning Curve 90

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

AI Assistants · Collaboration · Productivity

76,967

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
67
Dependency
Built with
Dart 74%
Rust 24%
Updated 1 weeks ago
Go
32%
Apache 2.0

CubeSandbox

AI Agents · Developer Tools · Security

12,728

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
82
Repo Health
88
Technical
63
Dependency
Built with
Go 32%
Rust 26%
C 22%
Updated 1 weeks ago
Rust
65%
Other

GitButler

AI Development · Developer Tools · Devops

21,726

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
66
Dependency
Built with
Rust 65%
TypeScript 22%
Svelte 10%
Updated 4 days ago
Go
64%
MIT

hoop

Monitoring · Security

823

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
85
Repo Health
80
Technical
64
Dependency
Built with
Go 64%
Clojure 17%
JavaScript 11%
Updated 1 weeks ago
TypeScript
95%
MIT

melty

AI Code Assistants · Code Editors · Developer Tools

5,444

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
67
Dependency
Built with
TypeScript 95%
Updated 1 years ago
Rust
70%
AGPL 3.0

RustDesk

Networking

124,658

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

View details
93
Repo Health
84
Technical
71
Dependency
Built with
Rust 70%
Dart 22%
Updated 4 days ago
TypeScript
95%
Apache 2.0

Void

AI Code Assistants · Automation · Code Editors

28,786

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

View details
45
Repo Health
84
Technical
68
Dependency
Built with
TypeScript 95%
Updated 4 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