#help with enum matching

3 messages · Page 1 of 1 (latest)

wind quarry
#

can anybody help me with pattern matching i'm so confused with matching from docs.

/// Parsed header value.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub enum HeaderValue<'x> {
    /// Single address
    Address(Addr<'x>),

    /// Address list
    AddressList(Vec<Addr<'x>>),

    /// Group of addresses
    Group(Group<'x>),

    /// List containing two or more address groups
    GroupList(Vec<Group<'x>>),

    /// String
    Text(Cow<'x, str>),

    /// List of strings
    TextList(Vec<Cow<'x, str>>),

    /// Datetime
    DateTime(DateTime),

    /// Content-Type or Content-Disposition header
    ContentType(ContentType<'x>),

    Empty,
}```

how can i match this to get the first Single Address

```rust
pub struct Addr<'x> {
    pub name: Option<Cow<'x, str>>,
    pub address: Option<Cow<'x, str>>,
}```
glossy mural
#

What have you tried?

earnest steeple
#

Hope this snippet can help.``` let my_addr = HeaderValue::Address(Addr {
name: Some("Home".into()),
address: Some("382 Noth 4 Street".into()),
});

match my_addr {
    HeaderValue::Address(a) => {
        println!("Address variant found");

        if let Some(name) = a.name {
            println!("The address name is {}.", name);
        }

        if let Some(address) = a.address {
            println!("The address is {}.", address);
        }
    }
    _ => {
        println!("Other HeaderValue variants found.");
    }
}```