From 99f409d2f1e0c6f8c1c0412c5df489bb9719c365 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Thu, 14 Aug 2025 08:11:43 +0000 Subject: [PATCH] [Sync Iteration] rust/clock/1 --- solutions/rust/clock/1/src/lib.rs | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 solutions/rust/clock/1/src/lib.rs diff --git a/solutions/rust/clock/1/src/lib.rs b/solutions/rust/clock/1/src/lib.rs new file mode 100644 index 0000000..c1cec13 --- /dev/null +++ b/solutions/rust/clock/1/src/lib.rs @@ -0,0 +1,39 @@ +use std::fmt; + +pub struct Clock { + hours: i32, + minutes: i32, +} + +impl Clock { + pub fn new(hours: i32, minutes: i32) -> Self { + let total_minutes = 60 * hours + minutes; + + Self { + hours: (total_minutes as f64 / 60.0).floor().rem_euclid(24.0) as i32, + minutes: (total_minutes.rem_euclid(60)), + } + } + + pub fn add_minutes(&self, minutes: i32) -> Self { + Clock::new(self.hours, self.minutes + minutes) + } +} + +impl PartialEq for Clock { + fn eq(&self, other: &Self) -> bool { + self.hours == other.hours && self.minutes == other.minutes + } +} + +impl fmt::Display for Clock { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:02}:{:02}", self.hours, self.minutes) + } +} + +impl fmt::Debug for Clock { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:02}:{:02}", self.hours, self.minutes) + } +}