A pure function has no observable side effects, and its return value depends only on its input parameters. The same inputs therefore produce the same result, making the function easier to understand and use. section 7.2 section 7.12
fn sum_if_big_enough(a: i32, b: i32) -> i32 {
if a > 100 && b > 1000 {
a + b
} else {
0
}
}
This Rust example computes its result entirely from its parameters, without consulting external resources. section 7.2
Contrasting external state
A function that sums two numbers when they pass fixed thresholds remains pure. Replacing those checks with a database query makes it impure: the database can change, fail, or return a different answer for the same arguments. section 7.2
Use purity to simplify implementation and testing:
- Move transformations and business logic into pure functions where possible. section 6.5
- Keep impure code that communicates with databases or other outside parties small. section 6.5 section 7.2
- Test pure functions directly, reducing dependence on mocks or real external dependencies. section 6.5


















