An impure function depends on or produces effects beyond its input parameters. Its output can change when an external resource changes or fails. section 7.2
Database queries make a function impure because the same arguments can produce different results after database changes or failures. Calling an impure helper also makes its caller impure: the external dependency’s effects spread outward. section 7.2
Common effects include:
- Reading or modifying a database or communicating with another system. section 7.2
- Logging, which is usually a relatively safe side effect but can still fail because of operating-system problems. section 7.2
- Breaking ordinary control flow through panics or exception-like behavior; Result provides a more idiomatic error-handling alternative in Rust. section 7.0 section 7.12
Database-dependent summation
A function that sums two numbers only when a database-backed predicate approves them is impure. The predicate’s result may change between calls, even when both arguments remain unchanged. section 7.2
fn sum_if_big_enough(a: i32, b: i32) -> i32 {
if big_enough_according_to_database(a, b) {
a + b
} else {
0
}
}
Inspect the Rust example where an external database check replaces a deterministic condition and makes the surrounding function impure. section 7.2

