Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let n = 7;
let size = match n {
x if x < 0 => "negative",
0 => "zero",
1..=9 => "small",
_ => "large",
};
println!("{size}");
}small
- Expecting
negativebecausexfits any number: the guardif x < 0is part of the arm, and the arm runs only when the pattern fits and the guard is true. - Expecting
largeby reading1..=9as the two values 1 and 9: a range pattern covers every value between its ends, and..=includes the 9.
- What does the program print for
n = -3and forn = 10? - Why does the
matchstop compiling if the_arm is removed?