
Practice 50 Pattern Matching Coding Questions
Q1. Write a Rust program to match an integer and print whether it is positive, negative, or zero.
Input:
x = 5
Expected Output:
Positive
Q2. Write a Rust program to match a tuple (x, y) and print the values of x and y.
Input:
(x, y) = (1, 2)
Expected Output:
x: 1, y: 2
Q3. Write a Rust program to match an enum variant and print a message.
Input:
enum Message { Quit, Move, Write }
Message::Move
Expected Output:
Move
Q4. Write a Rust program to use pattern matching to check if a number is in the range of 1 to 10.
Input:
x = 7
Expected Output:
In range
Q5. Write a Rust program to match a string and print the corresponding day of the week.
Input:
day = "Monday"
Expected Output:
Today is Monday
Q6. Write a Rust program to match a number and print whether it is even or odd.
Input:
x = 3
Expected Output:
Odd
Q7. Write a Rust program to match a number with multiple ranges and print the corresponding message.
Input:
x = 50
Expected Output:
50-100
Q8. Write a Rust program to match an option and print the value if it is Some, or a default message if it is None.
Input:
option = Some(5)
Expected Output:
Value: 5
Q9. Write a Rust program to match on a tuple with three elements and print each element.
Input:
(1, 2, 3)
Expected Output:
1, 2, 3
Q10. Write a Rust program to match a vector and print whether it is empty or contains elements.
Input:
vec = vec![1, 2, 3]
Expected Output:
Not empty
Q11. Write a Rust program to match a result and handle Ok and Err values.
Input:
result = Ok(10)
Expected Output:
Success: 10
Q12. Write a Rust program to match a range and check if a number falls within a specific range.
Input:
x = 25
Expected Output:
In range 1-30
Q13. Write a Rust program to match an enum with multiple variants and execute different actions based on the variant.
Input:
enum Direction { Up, Down, Left, Right }
Direction::Left
Expected Output:
Move Left
Q14. Write a Rust program to use pattern matching to extract values from a tuple.
Input:
(x, y) = (10, 20)
Expected Output:
x: 10, y: 20
Q15. Write a Rust program to match an Option and return a default value if it is None.
Input:
option = None
Expected Output:
Default Value
Q16. Write a Rust program to match a string and return different messages for different strings.
Input:
status = "success"
Expected Output:
Operation was successful
Q17. Write a Rust program to use pattern matching to check if an integer is positive, negative, or zero.
Input:
x = -5
Expected Output:
Negative
Q18. Write a Rust program to match a Result type and handle errors gracefully.
Input:
result = Err("An error occurred")
Expected Output:
Error: An error occurred
Q19. Write a Rust program to match an enum with data inside the variant and print it.
Input:
enum Color { Red, Green, Blue(u8) }
Color::Blue(255)
Expected Output:
Blue with value 255
Q20. Write a Rust program to match a range of numbers and print whether the number is in the range.
Input:
x = 3
Expected Output:
In range 1-5
Q21. Write a Rust program to match on a tuple of two values and return the sum.
Input:
(3, 4)
Expected Output:
7
Q22. Write a Rust program to match a vector with different patterns.
Input:
vec = vec![1, 2, 3]
Expected Output:
Vec contains elements
Q23. Write a Rust program to match a number with multiple ranges and print the appropriate message.
Input:
x = 15
Expected Output:
15-20
Q24. Write a Rust program to match on a tuple and check the first value.
Input:
(1, "hello")
Expected Output:
First value is 1
Q25. Write a Rust program to match a struct with named fields and print its values.
Input:
struct Point { x: i32, y: i32 }
Point { x: 3, y: 4 }
Expected Output:
Point: x = 3, y = 4
Q26. Write a Rust program to match a result and unwrap the value if it is Ok, or handle the error.
Input:
result = Ok(100)
Expected Output:
Unwrapped value: 100
Q27. Write a Rust program to match a None value in an Option and handle it appropriately.
Input:
option = None
Expected Output:
No value present
Q28. Write a Rust program to match a vector and check if it contains at least one element.
Input:
vec = vec![1]
Expected Output:
Has at least one element
Q29. Write a Rust program to match a tuple with three elements and return the middle element.
Input:
(1, 2, 3)
Expected Output:
2
Q30. Write a Rust program to match an enum and return a message for each variant.
Input:
enum Status { Active, Inactive, Suspended }
Status::Suspended
Expected Output:
Account Suspended
Q31. Write a Rust program to match a string and return an appropriate message for different day names.
Input:
day = "Friday"
Expected Output:
It's Friday, time to relax!
Q32. Write a Rust program to match a tuple of two integers and return their product.
Input:
(3, 4)
Expected Output:
12
Q33. Write a Rust program to match a Some value and print it, otherwise print a default message.
Input:
option = Some(10)
Expected Output:
Value: 10
Q34. Write a Rust program to match an enum with a numeric value inside it and print the value.
Input:
enum Status { Success(i32), Failure(i32) }
Status::Success(200)
Expected Output:
Success with code 200
Q35. Write a Rust program to match a vector and print the first element if it exists.
Input:
vec = vec![5, 6, 7]
Expected Output:
First element is 5
Q36. Write a Rust program to match an integer and print a specific message for each value.
Input:
x = 5
Expected Output:
Five
Q37. Write a Rust program to match a number and print a message for each different case.
Input:
x = 0
Expected Output:
Zero
Q38. Write a Rust program to match a range of numbers and check if a number is within the range.
Input:
x = 8
Expected Output:
Within range 1-10
Q39. Write a Rust program to match a tuple with two elements and check if the first element is positive.
Input:
(10, -5)
Expected Output:
First element is positive
Q40. Write a Rust program to match an enum with data inside and handle different types.
Input:
enum MyEnum { A(i32), B(f64) }
MyEnum::B(3.14)
Expected Output:
Variant B with value 3.14
Q41. Write a Rust program to match a result and print whether it is successful or an error.
Input:
result = Err("Failure")
Expected Output:
Error: Failure
Q42. Write a Rust program to match a tuple with two elements and return the larger value.
Input:
(10, 20)
Expected Output:
20
Q43. Write a Rust program to match a string and print the first letter.
Input:
name = "Alice"
Expected Output:
A
Q44. Write a Rust program to match an integer and check if it is a multiple of 5.
Input:
x = 10
Expected Output:
Multiple of 5
Q45. Write a Rust program to match a string and check if it contains a specific substring.
Input:
name = "Rust Programming"
Expected Output:
Contains Rust
Q46. Write a Rust program to match a vector of numbers and return the sum of all the elements.
Input:
vec = vec![1, 2, 3]
Expected Output:
6
Q47. Write a Rust program to match a tuple with two elements and check if the sum is greater than 10.
Input:
(6, 5)
Expected Output:
Greater than 10
Q48. Write a Rust program to match a result and return the value if successful, or a default value if failed.
Input:
result = Ok(10)
Expected Output:
Value: 10
Q49. Write a Rust program to match a tuple and return a message based on the sum of its elements.
Input:
(5, 6)
Expected Output:
Sum is 11
Q50. Write a Rust program to match a vector and return a message if it contains exactly three elements.
Input:
vec = vec![1, 2, 3]
Expected Output:
Exactly three elements