If Let Enums

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

Click here to reveal hints
  • 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.