This commit is contained in:
2025-03-26 12:41:58 -04:00
parent 637b26a0e9
commit 416dd617e6
36 changed files with 732 additions and 0 deletions

1
example_5/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

6
example_5/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "example_5"
version = "0.1.0"
edition = "2021"
[dependencies]

27
example_5/src/main.rs Normal file
View File

@@ -0,0 +1,27 @@
struct Years(i64);
struct Days(i64);
impl Years {
pub fn to_days(&self) -> Days {
Days(self.0 * 365)
}
}
impl Days {
/// truncates partial years
pub fn to_years(&self) -> Years {
Years(self.0 / 365)
}
}
fn is_adult(age: &Years) -> bool {
age.0 >= 18
}
fn main() {
let age = Years(25);
let age_days = age.to_days();
println!("Is an adult? {}", is_adult(&age));
println!("Is an adult? {}", is_adult(&age_days.to_years()));
// println!("Is an adult? {}", is_adult(&age_days));
}