英文:
Issue with matching enum with options
问题
我需要解析邮件的解析器,并且我需要从中提取发件人的详细信息。我使用了Rust的mail_parser
库,其中通过from
方法提取了解析后的发件人信息。from
方法返回了一个名为HeaderValue
的枚举类型。但我不明白的是,如果我需要获取Address(Addr<'x>)
,Address
是一个包含以下选项的结构体:
pub struct Addr<'x> {
pub name: Option<Cow<'x, str>>,
pub address: Option<Cow<'x, str>>,
}
我遇到的问题是无法通过match
语句从Address(Addr<'x>)
中提取数据并获取选项中的name
和address
字段。我应该如何编写match
语句来实现这个目的?
英文:
I have this parser which used to parse mail and i need to parse the from details from i use rust mail_parser crate. there the parsed input from details are extracted using from
method the from
method returns an enum called HeaderValue
link
what i can't understand is if i need to get the Address(Addr<'x>)
the Address is an option which contains this option
pub struct Addr<'x> {
pub name: Option<Cow<'x, str>>,
pub address: Option<Cow<'x, str>>,
}
what i'm getting issue is can't make the match to get the data from the Address(Addr<'x>)
and extract the option. if i need to get the name
and address
fields to struct how can i make the match
statement?.
答案1
得分: 0
它不难,只是标准语法。你具体遇到了什么错误?
use std::borrow::Cow;
pub struct Addr<'x> {
pub name: Option<Cow<'x, str>>,
pub address: Option<Cow<'x, str>>,
}
pub enum HeaderValue<'x> {
Address(Addr<'x>),
AddressList(Vec<Addr<'x>>),
Empty,
}
fn main() {
let value = HeaderValue::Address(Addr{name: Some("somename".into()), address: None});
match value {
HeaderValue::Address(Addr{name, address}) => println!("Parsed name={:?} address={:?}", name, address),
_ => println!("Couldn't match"),
}
}
打印
> Parsed name=Some("somename") address=None
或者对匹配不同组合有更多控制的话:
```rust
match value {
HeaderValue::Address(Addr{name:Some(name), address:Some(address)}) => println!("Parsed both name={:?} address={:?}", name, address),
HeaderValue::Address(Addr{address:Some(address), ..}) => println!("Parsed only address={:?}", address),
HeaderValue::Address(Addr{name:Some(name), ..}) => println!("Parsed only name={:?}", name),
HeaderValue::Address(Addr{..}) => println!("Parsed empty"),
_ => println!("Couldn't match"),
}
打印
> Parsed only name="somename"
对评论的回答:
我认为你不能在单个 match
语句中实现这个,但你可以使用多个 if let
语句,像这样:
let value = HeaderValue::Address(Addr{name: Some("somename".into()), address: None});
if let HeaderValue::Address(Addr{address:Some(address), ..}) = &value {
println!("Parsed address={:?}", address);
}
if let HeaderValue::Address(Addr{name:Some(name), ..}) = &value {
println!("Parsed name={:?}", name);
}
打印:
> Parsed name="somename"
<details>
<summary>英文:</summary>
It's not any hard, just the standard syntax. What errors did you face exactly?
use std::borrow::Cow;
pub struct Addr<'x> {
pub name: Option<Cow<'x, str>>,
pub address: Option<Cow<'x, str>>,
}
pub enum HeaderValue<'x> {
Address(Addr<'x>),
AddressList(Vec<Addr<'x>>),
Empty,
}
fn main() {
let value = HeaderValue::Address(Addr{name: Some("somename".into()), address: None});
match value {
HeaderValue::Address(Addr{name, address}) => println!("Parsed name={:?} address={:?}", name, address),
_ => println!("Couldn't match"),
}
}
Prints
> Parsed name=Some("somename") address=None
Or for more control over matching different combinations:
match value {
HeaderValue::Address(Addr{name:Some(name), address:Some(address)}) => println!("Parsed both name={:?} address={:?}", name, address),
HeaderValue::Address(Addr{address:Some(address), ..}) => println!("Parsed only address={:?}", address),
HeaderValue::Address(Addr{name:Some(name), ..}) => println!("Parsed only name={:?}", name),
HeaderValue::Address(Addr{..}) => println!("Parsed empty"),
_ => println!("Couldn't match"),
}
Prints
> Parsed only name="somename"
## Answer to the comment:
I don't think you can do that with a single `match` statement, but you can do multiple `if let` statements like so
let value = HeaderValue::Address(Addr{name: Some("somename".into()), address: None});
if let HeaderValue::Address(Addr{address:Some(address), ..}) = &value {
println!("Parsed address={:?}", address);
}
if let HeaderValue::Address(Addr{name:Some(name), ..}) = &value {
println!("Parsed name={:?}", name);
}
Prints:
> Parsed name="somename"
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论