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
- Turn the hours into minutes by multiplying by 60. For 13 hours that is 780 minutes.
- Add the minutes. 780 + 47 = 827 minutes in all.
- Put that sum in parentheses. Multiplication runs before addition, so without parentheses the final
* 60would apply only tominutesand the hours would be left in minute units. Parentheses make the addition happen first. - Multiply by 60 to get seconds: 827 * 60 = 49620.
- 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 = 0andminutes = 5gives 300. - Minutes of 60 or more. Nothing in the formula requires
minutesto be below 60. Withminutes = 90andhours = 0the 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 * 60is correct too. It is only a different grouping of the same arithmetic.
Testing
hours = 1,minutes = 2prints3720.hours = 13,minutes = 47prints49620.hours = 0,minutes = 5prints300.hours = 2,minutes = 0prints7200.
