Exercism clock problem

Hello,

I have to implmement a clock and so far I have this :

pub struct Clock{
    hour: i32, 
    minutes: i32,
}

impl Clock {
    pub fn new(hours: i32, minutes: i32) -> Self {
        Clock {
            hour:  
        )
    }

    pub fn add_minutes(&self, minutes: i32) -> Self {
        unimplemented!("Add {} minutes to existing Clock time", minutes);
    }
}

how can I take care that the hours and the minutes are never above the 24 or 59

I know there are test that have invalid numbers where I have to make them valid


#[test]
#[ignore]
fn test_hour_rolls_over() {
    assert_eq!(Clock::new(25, 0).to_string(), "01:00");
}

#[test]
#[ignore]
fn test_hour_rolls_over_continuously() {
    assert_eq!(Clock::new(100, 0).to_string(), "04:00");
}

#[test]
#[ignore]
fn test_sixty_minutes_is_next_hour() {
    assert_eq!(Clock::new(1, 60).to_string(), "02:00");

Hint:

fn main() {
    for i in 0..15 {
        println!("{} {}", i / 3, i % 3);
    }
}

click here to run

The % operator is called the modulo operator.

1 Like

Also check out the standard lib's i32::div_euclid() and i32::rem_euclid() methods to use other people's code to handle negative numbers :wink:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.