Problem overview
The function legal_age takes an age and returns a bool: true when the person is 18 or older and false otherwise. The starter code is empty, but the lesson is about a common first draft of this function and how to shorten it.
Approach: the comparison is already a boolean
A first draft usually looks like this:
fn legal_age(age: i32) -> bool {
if age >= 18 {
return true;
} else {
return false;
}
}
It works, but it says the same thing twice. The expression age >= 18 already evaluates to true or false. The if inspects that value only to produce the same value again. When the condition is true the function returns true, and when it is false it returns false, so the condition itself is the answer.
Any comparison (==, !=, <, <=, >, >=) is an expression with a bool value, and so is anything built from them with &&, || and !. An expression like that can be returned, stored in a variable or passed to a function, just like a number.
Why the longer form is worth avoiding
Beyond the extra lines, the long form invites mistakes. Every added line is one more place to return the wrong literal, and a reader has to check both branches to be sure the function does what its name says. With return age >= 18; there is one line and one rule to verify. The shorter version also describes the function the way you would say it aloud: "it returns whether the age is at least 18."
Step by step
- Drop the
ifand theelse. They only repeat the value of the condition. - Return the comparison. Write
age >= 18afterreturn. The function's return type isbool, which is what the comparison produces, so the types match without any conversion.
The solution in Rust
impl Solution {
fn legal_age(age: i32) -> bool {
return age >= 18;
}
}
Rust details worth knowing:
- Inclusive boundary. The operator is
>=, so exactly 18 counts.legal_age(18)istrue,legal_age(17)isfalse, andlegal_age(40)istrue. - The idiomatic form. In Rust the last expression of a function, written without a semicolon, is its return value. So the body can be just
age >= 18, with noreturn. The lesson uses an explicitreturnwith a semicolon, which is also valid. Do not mix the two up:age >= 18;with a semicolon and noreturnmakes the body evaluate to nothing, and the compiler reports a type mismatch. - No parentheses needed. The comparison
>=binds tighter thanreturnand the semicolon, so nothing extra is required.
A second example
The same reduction applies to any function that answers a yes-or-no question. A function that checks whether a number is even can return n % 2 == 0 directly, and one that checks whether two values differ can return a != b. If the long version of such a function has an if whose branches return only true and false, the condition can replace the whole statement.
Complexity
Time and space are both O(1): one comparison and no extra storage.
Edge cases and common mistakes
- Using
>instead of>=. That makes 18 returnfalse. - Returning the opposite. If the question is reversed, for example "is this person a minor?", return
age < 18, or negate a condition with!. Do not writeif cond { false } else { true };!condsays the same in less space. - The shortcut is only for boolean results. If the two branches return different non-boolean values, such as two strings, you still need the
if/else. The trick applies when the branches returntrueandfalse. - Negative ages. The comparison treats them like any other number:
legal_age(-5)isfalse.
Testing
age = 17givesfalse.age = 18givestrue.age = 40givestrue.age = 0givesfalse.
