Test private Rust functions by adding a #[cfg(test)] module and using use super::* to access internal items.
Add the #[cfg(test)] attribute to your test module and use use super::*; to access private functions within that module.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_private_function() {
let result = private_helper();
assert_eq!(result, 42);
}
}
fn private_helper() -> i32 {
42
}
Rust hides private functions from outside their module, but test modules are treated as part of the same module. This lets you run tests directly on internal logic without exposing it to the public. Think of it like a quality control inspector having a special key to check the factory floor without opening the doors to the public.