025 — Leap Year
Tutorial
The Problem
Determine whether a year is a leap year using the Gregorian calendar rule: a year is a leap year if it is divisible by 400, or divisible by 4 but not by 100. Express this as a single boolean expression and verify on representative examples.
🎯 Learning Outcomes
|| and && and correct operator precedenceu32 type for years with % modulo operatormod and <> operatorsCode Example
#![allow(clippy::all)]
// Placeholder — pending conversionKey Differences
| Aspect | Rust | OCaml |
|---|---|---|
| Modulo | % | mod |
| Equality | == 0 | = 0 |
| Not equal | != 0 | <> 0 |
| Type | u32 | int |
| Boolean ops | &&, \|\| | &&, \|\| |
| Expression length | Same | Same |
The leap year problem is one of the simplest demonstrations of translating between Rust and OCaml — the algorithm is identical, only the surface syntax changes. The canonical single-expression form is preferred over branching because it maps directly to the mathematical definition.
OCaml Approach
let leap_year year = (year mod 400 = 0) || (year mod 4 = 0 && year mod 100 <> 0) is identical in structure. The differences are purely syntactic: mod vs %, = vs ==, <> vs !=. Both implementations are equivalent, correct, and O(1).
Full Source
#![allow(clippy::all)]
// Placeholder — pending conversionExercises
days_in_february(year: u32) -> u32 returning 29 for leap years and 28 otherwise.is_leap_year_julian(year: u32) -> bool using only the 4-year rule (used before 1582).filter and count.(year+1..).find(|&y| is_leap_year(y)).years_until_next_leap : int -> int that returns how many years until the next leap year.