Skip to content

Clock

Clock in Rust on Exercism

概要

  • i32のhours, minutesが与えられるのでClock構造体を作成する

You will also need to implement .to_string() for the Clock struct. We will be using this to display the Clock’s state. You can either do it via implementing it directly or using the [Display trait][display-trait].

If so, try implementing the [Display trait][display-trait] for Clock instead.

初期コード

pub struct Clock;
impl Clock {
pub fn new(hours: i32, minutes: i32) -> Self {
todo!()
}
pub fn add_minutes(&self, minutes: i32) -> Self {
todo!()
}
}

考察

  • i32なので、negative valueも考慮する
    • 0 <= hours < 24
    • 0 <= minutes < 60
    • なので, MAXを持たせて加算してから mod で計算する
  • 24時間表記にする
  • add_minutes は特に気にすることはなさそう
  • 出力のためのstd::fmt::Displayを実装する

学んだこと

参考記事