Beta

Sometimes, you only need to handle a specific case of an enum variant, in this case using a match statement can be overkill. Rust provides a more concise way to handle such cases using the if let construct. This allows you to match a single variant of an enum and extract its value in a single line.

Here's an example:

enum Ip {
V4(String),
V6(String),
}
if let Ip::V4(ip) = &ip {
println!("IPv4 address: {}", ip);
}

Your Task

You are provided an enum called Message with the following variants:

  • Text(String): Represents a textual message.
  • Number(i32): Represents a numerical message.
  • Quit: Represents a command to quit.
  • None: Represents no message.

Your task is to implement the function process_text_message that takes a reference to a Message enum. For the Text variant, the function should return "Processed Text: <content>", replacing <content> with the actual string.

If the input is any other variant of the enum, the function should return "Unhandled Message".

Try to solve this using the if let construct.


Hints

<details> <summary>Click here to reveal hints</summary>
  • You can use the if let construct like this:
    if let EnumVariant(value) = &enum_instance {
    // Perform actions with `value`.
    }
  • Ensure you return "Unhandled Message" for all other variants not explicitly matched.
</details>
pub enum Message {
Text(String),
Number(i32),
Quit,
None,
}
pub fn process_text_message(message: &Message) -> String {
// Your code here...
String::from("Unhandled Message")
}
pub fn main() {
assert_eq!(
process_text_message(&Message::Text(String::from("Hello"))),
"Processed Text: Hello"
);
assert_eq!(
process_text_message(&Message::Number(42)),
"Unhandled Message"
);
assert_eq!(process_text_message(&Message::Quit), "Unhandled Message");
assert_eq!(process_text_message(&Message::None), "Unhandled Message");
}