Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trait implementation not found in Rust 1.70

Tags:

rust

traits

I was using the srx crate and it was working correctly under Rust 1.61. Now, I updated to Rust 1.70 and it cannot find the FromStr trait implementation.

An example code that works in 1.61 but not in 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())?;
}

and the compiler errors:

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`.

The trait in the library is implemented here.

I'm pretty new to Rust, so I'm not sure if I'm doing something wrong or the library is not implementing it correctly for the new rust version.

like image 523
ZJaume Avatar asked Oct 25 '25 01:10

ZJaume


1 Answers

If you look at the trait implementation of FromStr for SRX, you'll see that it is guarded by a 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)
    }
}

This is also indicated in the docs with a violet box stating that:

This is supported on crate feature from_xml only.

To fix this, you need to enable the from_xml feature for the srx crate in your Cargo.toml file:

[dependencies]
srx = { version = "0.1", features = ["from_xml"] }
like image 91
Jonas Fassbender Avatar answered Oct 26 '25 23:10

Jonas Fassbender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!