I ran the leap-year check on negative years, where the remainders come out negative, and it got all four right.
Twenty-two problems in the practice repository, and this is the one where I cannot find a Rust-specific decision. I looked for one. I looked for the allocation, the borrow, the panic path, the overflow, the integer width. The whole function is one expression and it evaluates to a bool.
impl Solution {
pub fn is_leap_year(year: i32) -> bool {
// Divisible by 4
// AND
// NOT divisible by 100
// OR divisible by 400
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}
}
This is the problem statement’s third hint, character for character. If I had been handed this problem in Python I would have typed the same characters, and nothing about the answer would have changed.
That is worth saying plainly, because most of the rest of this series is about the gap between what a Rust program looks like and what a Python program looks like, and here the gap is zero.
The clause people forget
The logic is not trivial, it is just not language-specific. The Gregorian rule has three clauses and the third one is the one people forget: a year divisible by 100 is not a leap year unless it is also divisible by 400.
That exception is why 1900 is not a leap year and 2000 is.
The repo’s test suite covers exactly the three cases that distinguish the clauses:
#[test]
fn test_is_leap_year() {
for (year, res) in [(2000, true), (1900, false), (2024, true)] {
assert_eq!(Solution::is_leap_year(year), res);
}
}
2024 exercises the plain divisible-by-4 branch, 1900 exercises the exception that makes it false, 2000 exercises the exception that makes it true. For a three-clause boolean expression, one input per clause is complete coverage, and I would not want a fourth case here.
The problem is settled by those three numbers and nothing else in the function has any say.
The two places Rust could have bitten
I went looking for the language, and there were exactly two candidate edges. Both of them are real properties of Rust, and both of them turn out not to apply here.
Overflow in the modulo. Rust’s % panics on one input: i32::MIN % -1, because the quotient is not representable. I confirmed that rustc rejects that expression at compile time, before the program ever runs, with this operation will panic at runtime and attempt to compute i32::MIN % -1_i32, which would overflow.
So the language statically forbids the only modulo that could fail. The divisors in this function are the constants 4, 100, and 400. None of them is -1, so the question never arises. i32::MIN % 4 is 0, not a panic.
The sign of the remainder. Rust’s % keeps the sign of the dividend, the way C does, so -7 % 2 is -1 and not 1. Python’s floor-mod gives 1 for the same expression.
Here it makes no difference, and the reason is in the shape of the expression rather than in the language: every comparison is against zero. year % 4 == 0, year % 100 != 0, year % 400 == 0. A negative remainder is still not zero, and a negative remainder that is not zero is still not equal to zero. The sign cannot reach the answer.
Negative years are outside the problem’s 1 <= year <= 10^4 range, but they are an obvious thing to try, so I ran them:
is_leap_year(-2024) = true
is_leap_year(-2000) = true
is_leap_year(-1900) = false
is_leap_year(0) = true
All correct. -1900 % 400 is -300, which is not zero, so 1900 stays a non-leap year no matter which side of zero you put it on.
The only thing I would change
The comment block above the expression is a transcription of the expression:
// Divisible by 4
// AND
// NOT divisible by 100
// OR divisible by 400
That is not a comment about anything the code does not already say. It is the same information in a different notation. I would keep it, because the parenthesized source is genuinely harder to parse at a glance than the indented English version, and that is a formatting win rather than an explanation.
But there is a signal in it: when the comments in a file have nothing left to add, the function has stopped having structure worth talking about.
What Rust contributed here
The rest of this series is about things the language forced: a guard that could not fire, a modulo that kept the wrong sign, a rounding that was not one. None of those are here. There is no allocation, nothing to borrow, no lifetime to name, no Result to propagate, no overflow to defend against.
I could inflate this to the usual length by talking about boolean short-circuit evaluation or about bool being a zero-sized type, but that would be writing about Rust adjacent to the solution rather than about the solution.
The honest claim is that this problem has nothing to teach me about Rust, so the honest length of this post is short.
The rule I am taking from the series: I write the Rust-specific post when the language forced something. When it did not, I write the short one.