我使用的是srx crate ,它在铁 rust 1.61下工作正常.现在,我更新到Rust 1.70,它无法找到FromStr特征实现.

一个在1.61中工作但在1.70中不能工作的示例代码:

use std::fs::read_to_string;
use std::fs;
use std::str::FromStr;
use srx::SRX;

fn main() {
    let srx_file = "../data/language_tool.segment.srx";
    let _srx2: SRX = read_to_string(srx_file).expect("").parse().unwrap();
    let _srx1 = SRX::from_str(&fs::read_to_string(srx_file).unwrap())?;
}

和编译器错误:

error[E0277]: the trait bound `SRX: FromStr` is not satisfied
 --> src/main.rs:8:58
  |
8 |     let _srx2: SRX = read_to_string(srx_file).expect("").parse().unwrap();
  |                                                          ^^^^^ the trait `FromStr` is not implemented for `SRX`
  |
  = help: the following other types implement trait `FromStr`:
            IpAddr
            Ipv4Addr
            Ipv6Addr
            NonZeroI128
            NonZeroI16
            NonZeroI32
            NonZeroI64
            NonZeroI8
          and 31 others
note: required by a bound in `core::str::<impl str>::parse`
 --> /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/str/mod.rs:2352:5

error[E0599]: no function or associated item named `from_str` found for struct `SRX` in the current scope
 --> src/main.rs:9:22
  |
9 |     let _srx1 = SRX::from_str(&fs::read_to_string("data/segment.srx").unwrap())?;
  |                      ^^^^^^^^ function or associated item not found in `SRX`

warning: unused import: `std::str::FromStr`
 --> src/main.rs:3:5
  |
3 | use std::str::FromStr;
  |     ^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.

库中的特性是here实现的.

我对Rust非常陌生,所以我不确定是我做错了什么,还是库没有为新的Rust版本正确地实现它.

推荐答案

如果您查看SRXFromStr的特征实现,您将看到它由一个feature:

#[cfg_attr(docsrs, doc(cfg(feature = "from_xml")))] // only implement FromStr if the from_xml feature is enabled
impl FromStr for SRX {
    type Err = Error;
    fn from_str(string: &str) -> Result<Self, Self::Err> {
        schema::from_str(string)
            .map_err(Error::from)
            .and_then(SRX::try_from)
    }
}

这也在docs中用一个紫罗兰色方框表示:

这仅在crate feature 100上受支持.

要解决此问题,您需要为Cargo.toml文件中的srx箱启用from_xml功能:

[dependencies]
srx = { version = "0.1", features = ["from_xml"] }

Rust相关问答推荐

给定使用newype习语定义的类型上的铁 rust Vec,有没有方法获得底层原始类型的一部分?

如何容器化Linux上基于Rust的Windows应用程序的编译过程?

如何创建引用构造函数拥有的变量的对象?

如何使用syn插入 comments ?

替换可变引用中的字符串会泄漏内存吗?

如何计算迭代器适配器链中过滤的元素的数量

RUST应用程序正在退出,错误代码为:(退出代码:0xc0000005,STATUS_ACCESS_VIOLATION)

无符号整数的Rust带符号差

在Rust中声明和定义一个 struct 体有什么区别

Rust 并行获取对 ndarray 的每个元素的可变引用

在没有任何同步的情况下以非原子方式更新由宽松原子操作 Select 的值是否安全?

Option<&T> 如何实现复制

如何保存指向持有引用数据的指针?

Rust:`sort_by` 多个条件,冗长的模式匹配

内部值发生变化时 Rc 的行为

无法理解 Rust 对临时值的不可变和可变引用是如何被删除的

如何展平以下嵌套的 if let 和 if 语句?

SDL2 没有在终端键上触发?

字符串切片的向量超出范围但原始字符串仍然存在,为什么判断器说有错误?

当值是新类型包装器时,对键的奇怪 HashMap 生命周期要求