I’m in love with my Rust, turning angel wings to dust 😁

Sing to the tune of “I wish I had an angel” from Nightwish. The more of Rust I am learning, the more I fall in love with it.

Behold this beautiful and expressive use of the match keyword:

let leapyear: bool = match (year % 100, year % 400, year % 4) {
    (0, 0, 0) => true,
    (0, _, 0) => false,
    (_, _, 0) => true,
    (_, _, _) => false,
};

As an exercise I am working on a little program that allows me to verify my results from calculating the weekday for a given date in my head according to the methods laid out in Mind Performance Hacks ((Ron Hale-Evans, 2006)1 and In 7 Tagen zum menschlichen Kalender (Jan van Koningsveld, 2013) 2.

The above expresses the rules for a leap year which in plain English are:

  • (_, _, 0) If the year is divisible by 4 (without remainder), it is a leap year, unless
  • (0, _, 0) … it is also divisible 100 in which case it is not a leap year, unless
  • (0, 0, 0) … it is also divisible by 400, in which case it is a leap year
  • (_, _, _) Any year not matching the above is not a leap year

The underscore (_) matches any value that isn’t explicitly given.

  1. ISBN 978 0596 101534 []
  2. 978 1484 113660 []
This entry was posted in EN, Programming, Rust and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *