Problem overview
The function allow_to_contest receives min_age, max_age and the age of a child. It returns true if the child may take part, which means age lies between min_age and max_age, both ends included, and false otherwise.
With min_age = 7, max_age = 12 and age = 7, the answer is true: 7 is exactly the minimum, and the minimum is allowed. With min_age = 3, max_age = 8 and age = 10, the answer is false, because 10 is above the maximum.
Tracing the second example: 10 >= 3 is true, so the right side of && is evaluated, and 10 <= 8 is false. A true value combined with a false one by && is false, so the function returns false. In the first example both comparisons are true (7 >= 7 and 7 <= 12), so the result is true.
Approach
There are three equivalent ways to write it:
- Check for failure first: return
falseifage < min_age, returnfalseifage > max_age, and otherwise returntrue. - Use one
ifwith both conditions joined by&&, returningtrueinside it andfalseafter it. - Skip the
ifand return the combined condition itself.
The third form works because a comparison is already a bool, and so is the result of &&. An if that returns true when its condition is true and false otherwise adds nothing. This lesson uses the third form.
The condition has two parts: the age is not below the minimum (age >= min_age) and the age is not above the maximum (age <= max_age). Both must hold, which is what && means.
Step by step
- Compare against the lower bound with
age >= min_age. Use>=, not>, because a child of exactly the minimum age is allowed. - Compare against the upper bound with
age <= max_age, again inclusive. - Join the two with
&&, so the result istrueonly when both are true. - Return the result directly. No
ifis needed.
The solution in Rust
impl Solution {
fn allow_to_contest(min_age: i32, max_age: i32, age: i32) -> bool {
return age >= min_age && age <= max_age;
}
}
Rust details:
- Precedence. The comparison operators bind tighter than
&&, so the two comparisons are evaluated first and no parentheses are needed. - Short-circuiting.
&&stops as soon as the left side isfalse. Ifage >= min_ageis false, the second comparison is never evaluated. Here that changes nothing, because both comparisons are cheap and have no side effects. - Return style. Leaving off
returnand the semicolon, so the body is justage >= min_age && age <= max_age, is the more common Rust style. The explicitreturnis equally correct. - Chained comparisons don't exist. Writing
min_age <= age <= max_age, as you can in some other languages, is a compile error in Rust. Use two comparisons and&&.
The early-return version
The first approach is worth knowing, because it scales to checks that are too complicated for one expression:
fn allow_to_contest(min_age: i32, max_age: i32, age: i32) -> bool {
if age < min_age {
return false;
}
if age > max_age {
return false;
}
return true;
}
It gives the same answers, one rejection per if, with no else needed because each if returns. For two simple bounds, the single expression is shorter and easier to read.
Complexity
Time is O(1), with two comparisons at most. Space is O(1).
Edge cases and common mistakes
- Boundaries. With
min_age = 7andmax_age = 12, ages 7 and 12 returntrue, while 6 and 13 returnfalse. Strict comparisons would fail at the two ends. - Using
||instead of&&. The testage >= min_age || age <= max_ageis true for almost every age, since any number is either at least the minimum or at most the maximum (or both). The range needs both bounds to hold. - Equal bounds. If
min_ageequalsmax_age, only that exact age is allowed. - Swapped bounds. If
min_ageis greater thanmax_age, no age can satisfy both comparisons, so the result is alwaysfalse.
Testing
min_age = 7,max_age = 12,age = 7givestrue.min_age = 7,max_age = 12,age = 12givestrue.min_age = 7,max_age = 12,age = 6givesfalse.min_age = 3,max_age = 8,age = 10givesfalse.
