How to Use the cfg! and cfg_attr Macros for Conditional Compilation

Use #[cfg(test)] to include test modules and #[cfg_attr] to conditionally apply attributes like Debug during testing.

Use #[cfg(test)] to include code only during testing and #[cfg_attr(test, derive(Debug))] to apply attributes conditionally. Place the #[cfg(test)] attribute directly before the module or item you want to exclude from production builds, and use #[cfg_attr] with the condition and attribute syntax to modify items based on configuration flags.

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

#[cfg_attr(test, derive(Debug))]
struct Rectangle {
    width: u32,
    height: u32,
}