module-concept
Rust Platform-specific modules
Provide separate implementations behind one interface when target operating systems differ. Use target configuration rather than the build machine’s operating system. Include a fallback if the program should compile for other targets. Expected result (not executed): other Why: For a non-Windows host target, the fallback supplies the function; Windows prints windows. Common mistake: Assuming the fallback means Linux only.
Formal Syntax & Structure
rust syntaxrust
#[cfg(target_os = "windows")]Working Example
Example Snippetrust
#[cfg(target_os = "windows")]mod platform { pub fn label() -> &'static str { "windows" } }#[cfg(not(target_os = "windows"))]mod platform { pub fn label() -> &'static str { "other" } }fn main() { println!("{}", platform::label()); }