Add code
This commit is contained in:
1
example_5/.gitignore
vendored
Normal file
1
example_5/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
6
example_5/Cargo.toml
Normal file
6
example_5/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "example_5"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
27
example_5/src/main.rs
Normal file
27
example_5/src/main.rs
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user