Going through exercism. Tackling rust track initially
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
648 B

use std::fmt;
#[derive(Debug, PartialEq, Eq)]
pub struct Clock {
hours: i32,
minutes: i32
}
impl fmt::Display for Clock {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:02}:{:02}", self.hours, self.minutes)
}
}
impl Clock {
pub fn new(hours: i32, minutes: i32) -> Self {
Self { hours: (hours + minutes.div_euclid(60)).rem_euclid(24),
minutes: minutes.rem_euclid(60)
}
}
pub fn add_minutes(&self, minutes: i32) -> Self {
Clock::new(self.hours, self.minutes + minutes)
}
pub fn to_string(&self) -> String {
format!("{}", self)
}
}