This commit is contained in:
Anh Hoang Nguyen 2023-09-12 18:17:28 -04:00
parent 9ab6ccccb2
commit bac5b0d34b
3 changed files with 53 additions and 0 deletions

5
.gitignore vendored
View File

@ -14,3 +14,8 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Added by cargo
/target

12
Cargo.toml Normal file
View File

@ -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"

36
src/main.rs Normal file
View File

@ -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
}
}