Deleting one line from my prime checker turned is_prime(2) into false.

impl Solution {
    pub fn is_prime(n: i32) -> bool {
        // 2 is prime
        if n == 2 {
            return true;
        }
        // even is not prime
        if n < 2 || n % 2 == 0 {
            return false;
        }
        // Check odd divisors
        let limit = (n as f64).sqrt() as i32;
        let mut i = 3;
        // While our iterator, starting with 3 is
        // less than the sqrt of n
        while i <= limit {
            // if n is divisible by i, it's not prime
            if n % i == 0 {
                return false;
            }
            i += 2; // only check odd numbers
        }
        true
    }
}

The line I deleted was if n == 2 { return true; }. Put it back and the same call returns true. That single line is the difference between a correct primality test and one that thinks two is even.

Everything else in the function is a general rule. The general rule does not make exceptions.

Two has to be handled first

Because 2 % 2 == 0. Two is even, the even check rejects it, and the only way it survives is if it is handled before the general rule runs.

That is the entire design of the function, and it lives in the ordering of two if blocks rather than in any of the logic inside them. The special case has to be a guard clause in front of the general rule, not a special case buried inside it. Put the n == 2 check after the even check and it can never run, because the function has already returned.

Rust makes this an ordering problem and not a structural one. There is no exception hierarchy to fight, no subclass to construct, no try to catch. A guard clause is a statement that happens to come first, and the compiler does not care either way.

That is convenient.

It also means the ordering is entirely on me.

Nothing in the type system records that n == 2 is load-bearing.

The n < 2 half of the second guard is doing the same kind of work for the other end of the range. It rejects 1, 0, and every negative integer before the loop is reached, which matters because the loop’s true at the end is only correct for numbers that survived the guards.

The suite has never seen two

Here is the test:

#[test]
fn test_is_prime() {
    for (num, boolean) in [(7, true), (4, false), (1, false)] {
        assert_eq!(Solution::is_prime(num), boolean);
    }
}

Three cases: seven is prime, four is not, one is not. Two is not in the list.

So I removed the n == 2 block from the crate and ran cargo test. One passed, zero failed. The suite is completely indifferent to whether the function can identify the smallest prime number, because every input it uses is answered by the general rule.

What is worth keeping is the shape of the miss: the function has a special case, and the test suite covers the general path three times over and the special path zero times.

If you are going to carry a special case, the test that matters is the one that only passes because the special case is there. Adding (2, true) to that list is the whole fix. It fails on the version without the guard and passes on the version with it.

A float in the middle of an integer test

The loop bound is this line:

let limit = (n as f64).sqrt() as i32;

A float, in the middle of an integer primality test, deciding how far the integer loop should go. n as f64 is lossless because every i32 is well below 2^53, so that cast is exact. The as i32 truncates toward zero, so the result is the floor of the square root as long as the float square root is not slightly below an integer that it should have landed exactly on.

I checked that against an integer square root over 2,139,019 values: every perfect square in the i32 range and both of its neighbours, plus two million pseudo-random values. Zero mismatches. The float square root is correctly rounded, the true root of a non-square is far enough from an integer for a rounding error of a fraction of an ulp not to reach it, and the root of an exact square is exactly representable, so it is returned exactly. is_prime(2147117569), which is 46337 squared, correctly returns false, and is_prime(2147483647) correctly returns true.

So the float is safe here. Safe is not the same as exact, and an integer square root would be exact by construction rather than by a margin argument about the size of the range. The float version is a choice, and the choice is defensible for a practice crate where n is capped at 10^6.

It is worth naming because nothing in the code says which one it is relying on.

Halving the divisions

i += 2 with the loop starting at 3 is the last piece of the same design. Once 2 has been dealt with, n is known to be odd, and no even number can divide an odd number. Every even candidate from 4 upward is therefore a guaranteed miss, and the loop skips all of them.

The effect is to halve the number of divisions in the worst case, and it is only safe because of the guard above it. If n were still allowed to be even at that point, skipping even divisors would be wrong, since 4 divides 8 and the loop starting at 3 would never check it. The i += 2 is a consequence of the guards, not an independent optimization.

Three lines of the function are about the same fact: two is the only even prime. The n == 2 guard exists because of it, the n % 2 == 0 rejection exists because of it, and the i += 2 stride exists because of it.

What I am left wanting is a test for the case only the special path can pass. The suite does not have one yet, so the guard stays load-bearing and unverified.


Next: Rust’s Percent Keeps the Sign