std-module
std::os: isolate platform-specific extensions
The std::os module contains extensions available for particular targets, including Unix and Windows. Keep such code behind compile-time attributes and provide a fallback or explicitly reject unsupported targets. cfg! evaluates to a boolean but does not remove code from type checking. This example selects a platform-specific implementation without claiming all targets offer the same operations. Fixture: Expected stdout (not executed): platform boundary selected Expected stderr: (empty) Expected exit: 0 Common mistake: Importing a Unix-only extension unconditionally in a portable program.
Formal Syntax & Structure
rust syntaxrust
#[cfg(unix)] / #[cfg(windows)]Working Example
Example Snippetrust
fn main() { #[cfg(unix)] fn native_separator() -> char { std::path::MAIN_SEPARATOR } #[cfg(windows)] fn native_separator() -> char { std::path::MAIN_SEPARATOR } #[cfg(not(any(unix, windows)))] fn native_separator() -> char { std::path::MAIN_SEPARATOR } assert_eq!(native_separator(), std::path::MAIN_SEPARATOR); println!("platform boundary selected");}