From bac5b0d34b42bd470088c04480f7b6695ce45bd3 Mon Sep 17 00:00:00 2001 From: Anh Hoang Nguyen Date: Tue, 12 Sep 2023 18:17:28 -0400 Subject: [PATCH] init --- .gitignore | 5 +++++ Cargo.toml | 12 ++++++++++++ src/main.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 3ca43ae..193d30e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,8 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..eaaee7c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "webhook-test" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +chrono = "0.4.30" + +[dev-dependencies] +chrono = "0.4.30" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f24c318 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,36 @@ +use chrono::Datelike; + +pub struct User<'a> { + name: &'a str, + dob: u32, +} + +impl<'a> User<'a> { + pub fn new(name: &'a str, dob: u32) -> User<'a> { + User { name, dob } + } + + pub fn age(&self) -> u32 { + let current_year = chrono::Utc::now().year() as u32; + current_year - self.dob + } +} + +fn main() { + let user = User::new("Anh Nguyen", 1999); + + println!("User name: {}", user.name); + println!("User age: {}", user.age()); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_user_age() { + let user = User::new("Test User", 2000); + let age = user.age(); + assert_eq!(age, 23); // Adjust the expected age based on the current year + } +}