Skip to content

Instantly share code, notes, and snippets.

View jan53n's full-sized avatar
:octocat:
Focusing

Jansen jan53n

:octocat:
Focusing
View GitHub Profile
@de-sh
de-sh / match_rs.md
Last active June 13, 2020 07:01
Rewriting if-else conditionals as match statements

It is common in computing, to come across a conditional statement that contains a lot of ANDs, ORs and more. With rust-lang's powerful match statement, a programmer can easily convey the message, let's see how.

match statements takes pattern matching to another level. Using the simple syntax, one can also achieve what nested/ladder if-else blocks could. Here is a sample if-else code block.

if year%4 == 0 {
  if year%100 != 0 || year%400 == 0 { 
    println!("Leap Year"); 
  } else { 
    println!("Not a Leap Year"); 
  }