Skip to content

Commit

Permalink
Moved rust code to its own cargo
Browse files Browse the repository at this point in the history
  • Loading branch information
RitchieFlick committed May 5, 2024
1 parent 5b44577 commit f42f04a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
book
.DS_Store
listings/target
target
43 changes: 1 addition & 42 deletions src/software_development/is-leap-year.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
**Rust**

```rust
#let year = 2020;
let is_leap = match (year % 400, year % 100, year % 4) {
(0, _, _) => true,
(_, 0, _) => false,
(_, _, 0) => true,
_ => false,
};
#println!("Year {} is a leap year: {}", year, is_leap);
{{#rustdoc_include is-leap-year/src/main.rs:2:7}}
```

### Notes
Expand All @@ -23,40 +16,6 @@ The rules for determining if a year is a **leap year**:
- but years divisible by 100 are **not** leap years,
- but years divisible by 400 are leap years.

### Test cases

```rust,no_run,ignore
#[cfg(test)]
mod sample_tests {
use super::is_leap_year;
fn do_test(year: i32, expected: bool) {
let actual = is_leap_year(year);
assert_eq!(actual, expected, "\nYour result (left) does not match the expected output (right) for the year {year:?}");
}
#[test]
fn year_2020_is_a_leap_year() {
do_test(2020, true);
}
#[test]
fn year_2000_is_a_leap_year() {
do_test(2000, true);
}
#[test]
fn year_2015_is_not_a_leap_year() {
do_test(2015, false);
}
#[test]
fn year_2100_is_not_a_leap_year() {
do_test(2100, false);
}
}
```

## Chrestomathy

**SQL**
Expand Down
7 changes: 7 additions & 0 deletions src/software_development/is-leap-year/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/software_development/is-leap-year/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "is-leap-year"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
42 changes: 42 additions & 0 deletions src/software_development/is-leap-year/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
fn is_leap_year(year: i32) -> bool {
match (year % 400, year % 100, year % 4) {
(0, _, _) => true,
(_, 0, _) => false,
(_, _, 0) => true,
_ => false,
}
}

fn main() {
println!("Year {} is a leap year: {}", 2020, is_leap_year(2020));
}

#[cfg(test)]
mod sample_tests {
use super::is_leap_year;

fn do_test(year: i32, expected: bool) {
let actual = is_leap_year(year);
assert_eq!(actual, expected, "\nYour result (left) does not match the expected output (right) for the year {year:?}");
}

#[test]
fn year_2020_is_a_leap_year() {
do_test(2020, true);
}

#[test]
fn year_2000_is_a_leap_year() {
do_test(2000, true);
}

#[test]
fn year_2015_is_not_a_leap_year() {
do_test(2015, false);
}

#[test]
fn year_2100_is_not_a_leap_year() {
do_test(2100, false);
}
}

0 comments on commit f42f04a

Please sign in to comment.