How to Use cfg Attributes for Platform-Specific Code

Use the #[cfg(...)] attribute to include or exclude code blocks based on the target operating system or build configuration.

Use the #[cfg(...)] attribute to conditionally compile code based on the target platform or build configuration. Place the attribute directly above the function, module, or item you want to include only under specific conditions.

#[cfg(target_os = "linux")]
fn linux_specific_setup() {
    // Linux-only code
}

#[cfg(target_os = "windows")]
fn windows_specific_setup() {
    // Windows-only code
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}