【问题标题】:creating an error to match nom::IResult in nom 6.2.1在 nom 6.2.1 中创建错误以匹配 nom::IResult
【发布时间】:2021-08-03 01:25:42
【问题描述】:

在 nom 5.0 中编译得很好:

fn escaped_space(i: &str) -> nom::IResult<&str, &str> {
    nom::combinator::value(" ", nom::bytes::complete::tag("040"))(i)
}
assert_eq!(escaped_space(" "), Err(nom::Err::Error((" ", nom::error::ErrorKind::Tag))));

但在当前版本 6.2.1 中,断言行无法编译并出现类型不匹配错误 [E0308]:

expected Result<(&str, &str), nom::Err<nom::error::Error<&str>>> 

found Result<_, nom::Err<(&str, nom::error::ErrorKind)>>

如何创建错误以使其现在与最上面的错误匹配?

【问题讨论】:

    标签: rust nom


    【解决方案1】:

    在 nom 6 中,有一个 additional struct 包含错误详细信息,而不是 nom 5 中的元组:

    Err( 
        nom::Err::Error(
            nom::error::Error::new( //the new struct, instead of the tuple
                " ",
                nom::error::ErrorKind::Tag
            )
        )
    )
    

    【讨论】:

      猜你喜欢
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 2020-07-18
      • 2018-07-30
      • 2016-02-02
      • 2022-01-23
      相关资源
      最近更新 更多