← Back to list

Leap Year With Rust

If you are reading this, probably you are looking for a way to implement a function or logic that can help determine if a provided year is…

Nehemie Niyomahoro · 2019-11-28 16:27 · 1 claps · 1.2 min read
#javascript #leap-year #rustlang #rust
Open on Medium ↗
Wiki topics: 🌐 · Web Development 📚 · Books & Reading

Leap Year With Rust

Photo by Sean Lim on Unsplash

Photo by Sean Lim on Unsplash

If you are reading this, probably you are looking for a way to implement a function or logic that can help determine if a provided year is a leap one or not using Rust or Javascript or any other language that can have the same syntax as Rust language.

Cool, how about we start with what is a leap year? Simply, it is a calendar year containing an additional day added to keep the calendar year synchronized with the astronomical or seasonal year. (source)

In terms of coding and implementation, it goes by one following the below logic while also respecting the order. (One has a star, another has star and — and another has a star — — )

** on every year that is evenly divisible by 4

    • except every year that is evenly divisible by 100 - - unless the year is also evenly divisible by 400

In a few words by breaking down the condition, the logic is

The main condition that shall be applied to all the rest is if the year is divisible by four 4, and then have two conditions go against each other as if not divisible by 100 or if by any case it is divisible by 100 should also be divisible by 400

Which leaves us with the below code

pub fn is_leap_year(year: u64) -> bool {
"This should return if a year is either a leap one or not";
return year % 4 == 0 && (year % 100 != 0 || (year % 100 == 0 && year % 400 == 0));
}

That is it, Thanks


메타데이터
post_id
64c8c40bd4fa
slug
leap-year-with-rust-64c8c40bd4fa
url
https://medium.com/@nehemie/leap-year-with-rust-64c8c40bd4fa
canonical_url
https://medium.com/@nehemie/leap-year-with-rust-64c8c40bd4fa
author_url
https://medium.com/@nehemie
status
ok
fetched_at
2026-07-29 09:46:26