Implementing Match Results in Rust
This challenge focuses on creating a robust system for representing and managing the outcomes of competitive matches. You will implement a Rust enum to clearly define different match results and a function to process these results, allowing for consistent and predictable handling of game outcomes. This is fundamental for any application dealing with scores, standings, or historical match data.
Problem Description
Your task is to implement a Rust program that can accurately represent and process the results of a match. A match can have several distinct outcomes: one player wins, the other player wins, or the match can end in a draw. You should also consider situations where a match might not have a definitive result yet (e.g., ongoing or postponed).
Specifically, you need to:
-
Define an
enum: Create an enum namedMatchResultthat can represent the following states:Player1Wins: Indicates player 1 has won.Player2Wins: Indicates player 2 has won.Draw: Indicates the match ended in a draw.Pending: Indicates the match result is not yet determined.Postponed: Indicates the match was postponed and will likely be rescheduled.
-
Implement a processing function: Create a function, let's call it
process_match_result, that takes aMatchResultenum value as input and returns aStringdescribing the outcome.- For
Player1Wins, the string should be "Player 1 won!". - For
Player2Wins, the string should be "Player 2 won!". - For
Draw, the string should be "The match was a draw.". - For
Pending, the string should be "Match result is pending.". - For
Postponed, the string should be "Match has been postponed.".
- For
Examples
Example 1:
Input: MatchResult::Player1Wins
Output: "Player 1 won!"
Explanation: The function correctly identifies that Player 1 won and returns the corresponding descriptive string.
Example 2:
Input: MatchResult::Draw
Output: "The match was a draw."
Explanation: The function handles the draw scenario, returning the appropriate message.
Example 3:
Input: MatchResult::Pending
Output: "Match result is pending."
Explanation: This demonstrates handling a non-conclusive match state.
Constraints
- The
MatchResultenum must contain exactly the five variants listed in the problem description. - The
process_match_resultfunction must accept aMatchResultand return aString. - The returned
Stringmust exactly match the specified output for eachMatchResultvariant. - No external crates are permitted for this challenge.
Notes
This challenge is designed to help you practice using Rust's powerful enum type and pattern matching (match expressions). Pay close attention to the exact string outputs required for each variant. Think about how match statements can elegantly handle all possible states of your MatchResult enum.