expression
Select a platform backend at compile time
Conditional compilation can remove unsupported platform code before type checking. Separate native implementations behind a shared interface instead of assuming all OS APIs are interchangeable. The Linux runner expects linux-fifo; other builds use their own label or an unsupported fallback. No native endpoint is created. Expected stdout (not executed): linux-fifo Common mistake: Using cfg! alone around calls that cannot compile for the selected target. Run only in the isolated fixture runner, not a public code playground.
Formal Syntax & Structure
rust syntaxrust
#[cfg(target_os = "linux")]Working Example
Example Snippetrust
#[cfg(target_os = "linux")]fn backend() -> &'static str { "linux-fifo" }#[cfg(target_os = "windows")]fn backend() -> &'static str { "windows-pipe" }#[cfg(not(any(target_os = "linux", target_os = "windows")))]fn backend() -> &'static str { "unsupported" }fn main() { println!("{}", backend()); }