Problem overview
This lesson has two functions. max_of_two is a helper that takes two integers and returns the larger one. max_of_three takes three integers and returns the largest, and it must do that by calling the helper rather than comparing numbers itself. For a = 3, b = 7, c = 5 the answer is 7. For -3, -8, -1 it is -1, the negative number closest to zero. For 4, 4, 2 it is 4: when two numbers tie, either one gives the same value.
Approach: build the answer from a smaller one
A helper is a small function that does one job so another function can reuse it. Here the job is "which of these two is larger?". The largest of three numbers is the larger of two things: the winner of the first pair, and the third number. So the plan is to compare a and b, then compare that winner with c. Both comparisons are the same job, so one helper covers both.
Step by step
- Return
awhen it is greater. Insidemax_of_two,if a > breturnsaimmediately. Areturnends the function on the spot. - Return
botherwise. The line after theifonly runs whenais not greater thanb. Thenbis larger, or the two are equal, and in both casesbis a correct answer. Noelseis needed. - Nest the calls in
max_of_three.Self::max_of_two(a, b)runs first and gives the larger of the first two. That value becomes the first argument of the outer call, which compares it withc.
The solution in Rust
impl Solution {
fn max_of_two(a: i32, b: i32) -> i32 {
if a > b {
return a;
}
return b;
}
fn max_of_three(a: i32, b: i32, c: i32) -> i32 {
return Self::max_of_two(Self::max_of_two(a, b), c);
}
}
Tracing a = 3, b = 7, c = 5: the inner call max_of_two(3, 7) returns 7, then the outer call max_of_two(7, 5) returns 7. The function returns 7.
Rust details worth knowing:
- Calling a function of the same
impl. These functions take noself, so they are associated functions, and a call needsSelf::in front of the name. Writing plainmax_of_two(a, b)fails withcannot find function `max_of_two` in this scope, and the compiler suggestsSelf::. - A call is an expression.
Self::max_of_two(a, b)has a value, ani32, so it can sit directly inside another call's argument list. - The helper's types. Both parameters and the return value are
i32, so the nested call type-checks without any conversion.
Why use a helper at all
The helper keeps each function small enough to read at a glance. max_of_two has one decision in it, and max_of_three has none of its own: it only says how the pieces fit together. The same helper would also work for four numbers, by nesting one more call, with no new comparison logic. When a bug shows up, there is exactly one place where numbers are compared, so there is exactly one place to look.
Edge cases and common mistakes
- Ties. With
a = 4andb = 4,a > bis false, so the helper returnsb. It is the same number asa, so the result is right. - Negative numbers. Comparison works the same way for them: -1 is greater than -3 and -8.
- The largest in any position. The result is 9 for
(1, 2, 9)and for(9, 2, 1). Because every number takes part in one of the two comparisons, it does not matter where the largest one sits. - Skipping the helper. Writing the comparisons directly inside
max_of_threemay give correct answers, but the lesson asks for the helper to do all the comparing.
Testing
(3, 7, 5)gives7.(-3, -8, -1)gives-1.(4, 4, 2)gives4.(1, 2, 9)gives9.(9, 2, 1)gives9.
