0
0
Rustprogramming~10 mins

Match guards in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print "small" when the number is less than 10.

Rust
let num = 5;
match num {
    x if x [1] 10 => println!("small"),
    _ => println!("big"),
}
Drag options to blanks, or click blank then click option'
A<
B>=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' in the guard.
Using '==' which only matches exactly 10.
2fill in blank
medium

Complete the code to print "even" when the number is even using a match guard.

Rust
let num = 8;
match num {
    x if x [1] 2 == 0 => println!("even"),
    _ => println!("odd"),
}
Drag options to blanks, or click blank then click option'
A/
B%
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%' which does division, not remainder.
Using '+' or '-' which are arithmetic operators but not for checking evenness.
3fill in blank
hard

Fix the error in the match guard to correctly check if the character is a vowel.

Rust
let ch = 'a';
match ch {
    c if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c [1] 'u' => println!("vowel"),
    _ => println!("consonant"),
}
Drag options to blanks, or click blank then click option'
A==
B!=
C=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' in the condition.
Using '!=' which checks for inequality.
4fill in blank
hard

Fill both blanks to create a match guard that matches numbers between 1 and 5 inclusive.

Rust
let num = 3;
match num {
    x if x [1] 1 && x [2] 5 => println!("between 1 and 5"),
    _ => println!("out of range"),
}
Drag options to blanks, or click blank then click option'
A>=
B<
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' excludes 5.
Using '>' instead of '>=' excludes 1.
5fill in blank
hard

Fill all three blanks to create a match guard that matches even numbers greater than 10.

Rust
let num = 12;
match num {
    x if x [1] 2 [2] 0 && x [3] 10 => println!("even and greater than 10"),
    _ => println!("other"),
}
Drag options to blanks, or click blank then click option'
A%
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%' for even check.
Using '<' instead of '>' for greater than check.
Using '=' instead of '==' for comparison.