Feature flags for conditional compilation

4915
0

Cargo's feature flags let you enable or disable parts of your crate at compile time. I use them for optional dependencies (like serde), platform-specific code, or different build profiles. Features are defined in Cargo.toml and checked with #[cfg(feature = "...")]. The default feature is enabled unless you opt out with --no-default-features. This keeps binary size small and compile times fast by excluding unused code. For libraries, features let users choose their dependencies. For example, a networking crate might have tokio and async-std as separate features. The compiler eliminates disabled code entirely, so there's zero runtime cost. It's a powerful tool for managing complexity in large projects.