I moved the first if in the fizzbuzz solution below the second one, and fifteen printed “Fizz”.

pub fn fizz_buzz(n: i32) -> Vec<String> {
    let mut res = Vec::new();
    for i in 1..=n {
        if i % 3 == 0 && i % 5 == 0 {
            res.push("FizzBuzz".to_string());
        } else if i % 3 == 0 {
            res.push("Fizz".to_string());
        } else if i % 5 == 0 {
            res.push("Buzz".to_string());
        } else {
            res.push(i.to_string());
        }
    }
    res
}

Here is the version I ran, and what it printed:

wrong branch order at 15 -> "Fizz"

No panic, no warning, no clue in the output that anything is wrong. Fifteen is the smallest number where the two orderings disagree, because it is the smallest number divisible by both three and five. Every input below it takes the same branch either way.

Two of these tests prove nothing

The file has three tests:

assert_eq!(Solution::fizz_buzz(3), vec!["1", "2", "Fizz"]);
assert_eq!(Solution::fizz_buzz(5), vec!["1", "2", "Fizz", "4", "Buzz"]);
assert_eq!(
    Solution::fizz_buzz(15),
    vec![
        "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz",
        "13", "14", "FizzBuzz"
    ]
);

Two of them cannot tell a correct implementation from a broken one. fizz_buzz(3) never reaches the first branch, because nothing in 1..=3 is divisible by five. fizz_buzz(5) never reaches it either, because the only multiple of five in 1..=5 is 5, and 5 is not divisible by three. Only the fifteen case exercises the first branch at all.

Delete the fifteen test and the file still passes against an implementation that gets fifteen wrong.

So the branch order is documented in exactly one place in this repository, and that place is an assertion instead of a comment.

Sixteen allocations for fifteen strings

Vec<String> means every element owns a heap buffer. I swapped in a global allocator that counts allocations:

shipped fizz_buzz(15): 16 fresh allocations, 2 reallocations, 784 bytes total

Sixteen fresh allocations for fifteen elements: one buffer per string, plus the vector’s own first allocation. Two more events are the vector growing from capacity 4 to 8 to 16. The strings themselves account for 112 bytes of capacity across the fifteen elements.

The literal branches are exact. “Fizz” comes out with a length of 4 and a capacity of 4, and “FizzBuzz” with 8 and 8.

The numeric branch is not:

1i32.to_string()        len=1 cap=10
7i32.to_string()        len=1 cap=10
42i32.to_string()       len=2 cap=10
12345i32.to_string()    len=5 cap=10
i32::MAX.to_string()    len=10 cap=10

Ten bytes of buffer for every integer, whatever its length. Eight of the fifteen elements in the output are numbers, and they claim 80 bytes of capacity to hold 11 characters.

Switching the literals to String::from changes nothing here. "Fizz".to_string() and String::from("Fizz") both give a length-4, capacity-4 string and one allocation, and the totals for fizz_buzz(15) are identical either way: 18 allocation events, 784 bytes.

The number branch is where the waste is. Here is my version, with format! instead of to_string:

format! variant  (15): 16 fresh allocations, 2 reallocations, 768 bytes total
format! variant  (1000): 1001 fresh allocations, 8 reallocations, 55452 bytes total

Against 784 and 56518 bytes for the shipped version. Same number of allocations, sixteen bytes less at n = 15 and a thousand bytes less at n = 1000.

Modest. The sort of thing that only shows up if you go looking for it.

What an assertion cannot see

Every assertion in that test file compares strings. The values are visible. The branch order is visible exactly once, through the one input in the whole file that carries evidence about it. The cost of producing the strings is invisible to all of them, and would stay invisible at any number of test cases.

Fifteen is the test. It is cheap, it runs in microseconds, and it is the only thing standing between this file and an implementation that quietly gets one number wrong.

If I reorder branches again, fifteen is the input I run first.


Next: The Reverse That Overflows