---
title: "Implement a function to check if a given year is a leap year or not using Rust."  
description: "Implement a function to check if a given year is a leap year or not using Rust."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158828/implement-a-function-to-check-if-a-given-year-is-a-leap-year-or-not-using-rust  
category: "rust"  
tags: ["programs", "rust"]  
reading_time: 2 minutes  

---

# Implement a function to check if a given year is a leap year or not using Rust.

Implement a [function to check](https://www.mindstick.com/forum/161900/write-a-function-to-check-if-a-number-is-prime-with-explanation) if a given year is a [leap year](https://www.mindstick.com/forum/156857/write-a-c-sharp-program-to-check-leap-year) or not using Rust.

## Replies

### Reply by Aryan Kumar

Sure, here is a Rust [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a given year is a leap year or not using Rust:

Rust

```plaintext
fn is_leap_year(year: i32) -> bool {
    if year % 400 == 0 {
        return true;
    } else if year % 100 == 0 {
        return false;
    } else if year % 4 == 0 {
        return true;
    } else {
        return false;
    }
}

fn main() {
    let year = 2020;
    let is_leap_year = is_leap_year(year);
    println!("The year {} is {}a leap year", year, if is_leap_year { "" } else { "not "));
}
```

This function first defines a function called is_leap_year that takes an i32 number as input and returns a bool value. The function checks if the year is a leap year by checking the following conditions:

- If the year is divisible by 400, then the year is a leap year.
- If the year is divisible by 100, but not by 400, then the year is not a leap year.
- If the year is divisible by 4, but not by 100, then the year is a leap year.
- Otherwise, the year is not a leap year.

The main function of the program then calls the is_leap_year function with the value 2020 as input. The result of the function call is then printed to the console.

To run the program, you can save it as a file called is_leap_year.rs and then compile it using the following command:

Code snippet

```plaintext
rustc is_leap_year.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./is_leap_year
```

This will print the following output to the console:

Code snippet

```plaintext
The year 2020 is a leap year
```


---

Original Source: https://www.mindstick.com/forum/158828/implement-a-function-to-check-if-a-given-year-is-a-leap-year-or-not-using-rust

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
