The #[cfg(test)] attribute includes code only during test execution, excluding it from production builds.
The #[cfg(test)] attribute tells the Rust compiler to include the following code block only when running tests.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
This keeps test code out of your production binary, reducing size and preventing accidental execution of test logic in your final application.
The cfg attribute is a special instruction to the Rust compiler that says, "Only include this code if we are running tests." It matters because it keeps your final software smaller and faster by excluding test code that isn't needed for real users. Think of it like a practice mode in a video game that only loads when you are training, not when you are playing the actual match.