Start learning free

LESSON 23 OF 414 · CALCULATIONS WITH NUMBERS

Convert Hours and Minutes into Seconds in Rust

Lesson Read the full explanation

We created two variables to store the number of hours and minutes we worked on a project. We want to convert this amount of time into seconds.

Use these two variables and some arithmetic operations to convert and print to the console the number of seconds the hours and minutes add up to.


Example:

For example, if hours was 1 and minutes was 2, the answer would be 3720.

Why? Because one hour and two minutes add up to 3720 seconds.


Need a hint?


1 hour is made up of 60 minutes. To how many minutes do hours and minutes add up?

hours and minutes add up to hours * 60 + minutes minutes.

Now, how many seconds are there in hours * 60 + minutes minutes?



As 1 minute is made up of 60 seconds, and we have (hours * 60 + minutes) minutes, the answer is: (hours * 60 + minutes) * 60


STEP 1 OF 1
    Ctrl + Enter to check

    RUST · CALCULATIONS WITH NUMBERS

    Convert Hours And Mins Into Seconds in Rust, explained

    Problem overview

    The program has two integer variables, hours and minutes, which record how long someone worked on a project. It must print the same amount of time in seconds.

    For hours = 1 and minutes = 2 the answer is 3720: one hour is 3600 seconds, two minutes are 120 seconds, and 3600 + 120 = 3720. In the editor the values are 13 hours and 47 minutes.

    Approach

    There are two natural ways to count, and they agree.

    Convert each unit separately. Hours times 3600 plus minutes times 60: hours * 3600 + minutes * 60.

    Go through minutes first. An hour is 60 minutes, so the total in minutes is hours * 60 + minutes. A minute is 60 seconds, so multiplying that total by 60 gives seconds: (hours * 60 + minutes) * 60. This form uses only the familiar number 60, and it mirrors how you would do it in your head.

    Unit conversions like this one are a chain: each step multiplies by how many of the smaller unit fit into the larger one. Hours become minutes with a factor of 60, and minutes become seconds with another factor of 60. Writing the chain out in that order, and checking each stage against a small example like 1 hour and 2 minutes, is the reliable way to get such formulas right.

    Step by step

    1. Turn the hours into minutes by multiplying by 60. For 13 hours that is 780 minutes.
    2. Add the minutes. 780 + 47 = 827 minutes in all.
    3. Put that sum in parentheses. Multiplication runs before addition, so without parentheses the final * 60 would apply only to minutes and the hours would be left in minute units. Parentheses make the addition happen first.
    4. Multiply by 60 to get seconds: 827 * 60 = 49620.
    5. Print the value with println! and a {} placeholder. The format string comes first, and the expression follows after the comma.

    The solution in Rust

    let hours: i32 = 13;
    let minutes: i32 = 47;
    
    println!("{}", (hours * 60 + minutes) * 60);
    

    This prints 49620. For the statement's example, (1 * 60 + 2) * 60 is 62 * 60, which is 3720.

    The parentheses matter

    Remove them and the meaning changes. hours * 60 + minutes * 60 is 780 + 2820 = 3600, which is neither the right answer nor even close, because the hours were converted to minutes but never to seconds. The parentheses are what tell Rust to finish the minute total first.

    Integer size

    The variables are i32, which holds values up to 2147483647. Since one hour is 3600 seconds, the result fits as long as the hours stay below 596,523. Past that, the multiplication overflows. In an optimized build (rustc -O), the result wraps around silently: with 600000 hours and 47 minutes the expression prints -2134964476. A debug build panics instead. Declaring the variables as i64 avoids the problem, and the same expression then gives the correct 2160002820.

    No real work log comes anywhere near that limit, so i32 is the sensible choice here. The point is only to know where the limit is, because the same kind of conversion, with millions of minutes or a count of milliseconds, is where this overflow shows up in practice.

    Edge cases and common mistakes

    • Zero hours or zero minutes. The formula still works: hours = 0 and minutes = 5 gives 300.
    • Minutes of 60 or more. Nothing in the formula requires minutes to be below 60. With minutes = 90 and hours = 0 the result is 5400, exactly 90 minutes in seconds.
    • Multiplying by 60 once. That leaves the answer in minutes. Seconds need the factor of 60 twice, in total 3600 per hour.
    • Dropping the parentheses, as shown above.
    • Writing a different unit. hours * 3600 + minutes * 60 is correct too. It is only a different grouping of the same arithmetic.

    Testing

    • hours = 1, minutes = 2 prints 3720.
    • hours = 13, minutes = 47 prints 49620.
    • hours = 0, minutes = 5 prints 300.
    • hours = 2, minutes = 0 prints 7200.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free