Start learning free

LESSON 82 OF 414 · CONDITIONAL STATEMENTS

Allow To Contest in Rust: Range Check with &&

Practice Read the full explanation

At a contest, children can participate only if they have ages between the minimum age and the maximum age (both included). Given the age of a child, check if they are allowed or not.

Example 1:

Input: minAge = 7, maxAge = 12, age = 7
Output: true

Example 2:

Input: minAge = 3, maxAge = 8, age = 10
Output: false

Hints:

You can solve it in 3 different ways:
1. By checking for invalidations and return false when age is below the minimum age and when it is above the maximum age, otherwise return true

2. Using one if statement with 2 conditions and return true, otherwise false

3. Since the function return value is a boolean, instead of using an if, you just return the condition since its value is a boolean.
STEP 1 OF 1
    Ctrl + Enter to check

    RUST · CONDITIONAL STATEMENTS

    Allow To Contest in Rust, explained

    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:

    1. Check for failure first: return false if age < min_age, return false if age > max_age, and otherwise return true.
    2. Use one if with both conditions joined by &&, returning true inside it and false after it.
    3. Skip the if and 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

    1. Compare against the lower bound with age >= min_age. Use >=, not >, because a child of exactly the minimum age is allowed.
    2. Compare against the upper bound with age <= max_age, again inclusive.
    3. Join the two with &&, so the result is true only when both are true.
    4. Return the result directly. No if is 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 is false. If age >= min_age is 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 return and the semicolon, so the body is just age >= min_age && age <= max_age, is the more common Rust style. The explicit return is 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 = 7 and max_age = 12, ages 7 and 12 return true, while 6 and 13 return false. Strict comparisons would fail at the two ends.
    • Using || instead of &&. The test age >= min_age || age <= max_age is 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_age equals max_age, only that exact age is allowed.
    • Swapped bounds. If min_age is greater than max_age, no age can satisfy both comparisons, so the result is always false.

    Testing

    • min_age = 7, max_age = 12, age = 7 gives true.
    • min_age = 7, max_age = 12, age = 12 gives true.
    • min_age = 7, max_age = 12, age = 6 gives false.
    • min_age = 3, max_age = 8, age = 10 gives false.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free