【问题标题】:Error while using public namespace quick_xml::se使用公共命名空间 quick_xml::se 时出错
【发布时间】:2021-01-02 23:30:18
【问题描述】:

我正在尝试使用quick_xml::se::to_string 函数。这似乎是一个公共函数,因为它是in the publicly available docs,quick_xml 源中被标记为pub,并且似乎是work fine in the quick_xml unit tests。这是我的代码:

use quick_xml::se::to_string as xml_to_string;
use serde::{Serialize};

#[derive(Debug, Serialize, Clone)]
struct ApiError<'a> {
    code: &'a str,
}

fn main() {
    xml_to_string(ApiError { code: "foo" })
}

但是,这失败了

error[E0432]: unresolved import `quick_xml::se`
 --> src/main.rs:1:16
  |
1 | use quick_xml::se::to_string as xml_to_string;
  |                ^^ could not find `se` in `quick_xml`

我尝试了其他几种导入此函数的方法(use quick_xml;use quick_xml::se::to_string),但似乎无论如何,我都会遇到同样的错误。这是怎么回事?

【问题讨论】:

    标签: rust serde


    【解决方案1】:

    为了能够使用quick-xmlse 模块,您需要启用serialize 功能。

    # Before:
    # quick-xml = "0.20.0"
    
    # After:
    quick-xml = { version = "0.20", features = ["serialize"] }
    

    如果您向下滚动到“Serde”,quick-xml 的自述文件中会提到这一点。在"Features" 下的文档中也提到了它。


    另外,quick_xml::se::to_string() 期望您需要添加 &amp; 的引用,例如:

    fn main() {
        xml_to_string(&ApiError { code: "foo" });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多