Start with the idea
#[cfg(...)] removes code before type checking, so a platform-specific branch costs nothing on other targets and does not even have to compile there. Cargo features are the same mechanism driven from the manifest, which is how one crate serves both a no_std embedded user and a full desktop one.
Conditional compilation decides which items exist in a particular build. A #[cfg(target_os = "wasi")] function is present only for matching targets; the opposite branch is present elsewhere. Cargo features are named inputs to this selection, often supplied by downstream users. Because a disabled branch is not part of the resulting item set, its behavior is not a runtime toggle and it must be checked in a build where it is enabled. Test the supported feature combinations rather than assuming the default build covers them.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.