我最近try 使用format_serde_error,这是一个格式化插入错误消息的 crate .我发现该示例似乎不能编译.以下代码片段:

use format_serde_error::SerdeError;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Foo {
    x: u64,
}

fn main() {
    let content = "x: 3";
    let foo = serde_yaml::from_str(content)
        .map_err(|err| SerdeError::new(content.to_string(), err))
        .unwrap();
    println!("Read in {:?}", foo);
}

连同随附的Cargo.toml个:

[package]
name = "ser-test"
version = "0.1.0"
edition = "2021"

[dependencies]
format_serde_error = { version = "0.3.0", features = ["serde_yaml"] }
serde = { version = "1.0.183", features = ["derive"] }
serde_yaml = "0.9.25"

产生令人费解的:

error[E0277]: the trait bound `ErrorTypes: From<serde_yaml::Error>` is not satisfied
   --> src/main.rs:12:61
    |
12  |         .map_err(|err| SerdeError::new(content.to_string(), err))
    |                        ---------------                      ^^^ the trait `From<serde_yaml::Error>` is not implemented for `ErrorTypes`
    |                        |
    |                        required by a bound introduced by this call
    |
    = help: the following other types implement trait `From<T>`:
              <ErrorTypes as From<serde_json::error::Error>>
              <ErrorTypes as From<serde_yaml::error::Error>>
              <ErrorTypes as From<(Box<(dyn StdError + 'static)>, std::option::Option<usize>, std::option::Option<usize>)>>
    = note: required for `serde_yaml::Error` to implement `Into<ErrorTypes>`
note: required by a bound in `SerdeError::new`
   --> /home/c/.cargo/registry/src/index.crates.io-6f17d22bba15001f/format_serde_error-0.3.0/src/lib.rs:278:41
    |
278 |     pub fn new(input: String, err: impl Into<ErrorTypes>) -> SerdeError {
    |                                         ^^^^^^^^^^^^^^^^ required by this bound in `SerdeError::new`

我之所以认为这是inscrutable,是因为在编译器输出中提到的code on github和lib.ars文件中,Impl中的这个‘Missing’被定义为:

#[cfg(feature = "serde_yaml")]
impl From<serde_yaml::Error> for ErrorTypes {
    fn from(err: serde_yaml::Error) -> Self {
        Self::Yaml(err)
    }
}

我不明白为什么编译器没有看到这From个Iml.我已经更新了铁 rust (运行1.73.0-nightly (f3623871c 2023-08-06))和cargo clean版,没有任何变化.如果我使用默认功能标志或指定serde_yaml,则此行为不会更改.

更令人困惑的是,我的代码完全(意外地)抄袭了format_serde_error提供的an example个内容.

对于那些想要准确重现该问题的人,以下是在我的机器上重现该问题的一系列shell 命令:

cargo new ser-test
cd ser-test
cargo add serde --features derive
cargo add serde_yaml
cargo add format_serde_error
echo Copy the snippet into main.rs here
cargo run

推荐答案

@ivan-c正确地发现了这个问题:format_serde_error使用的serde_yaml版本与我使用的版本不同.这导致了这个问题.解决方案有以下两种:

1.)明确指定serde_yaml版本0.8

2.)更新format_serde_error以使用较新的版本like this

Rust相关问答推荐

访问Rust中的隐藏变量

rust 蚀生命周期 行为

使用模块中的所有模块,但不包括特定模块

为昂贵的for循环制作筛子

在Rust 中移动原始指针的靶子安全吗

在复制类型中使用std::ptr::WRITE_VILAR进行内部可变性的安全性(即没有UnSafeCell)

如何轮询 Pin>?

Windows 上 ndarray-linalg 与 mkl-stats 的链接时间错误

在使用粗粒度锁访问的数据 struct 中使用 RefCell 是否安全?

如何在 `connect_activate()` 之外创建一个 `glib::MainContext::channel()` 并将其传入?

如何将一个矩阵的列分配给另一个矩阵,纳尔代数?

Rust 中的复合 `HashSet` 操作或如何在 Rust 中获得 `HashSet` 的显式差异/并集

通过写入 std::io::stdout() 输出不可见

Rust中是否可以在不复制的情况下从另一个不可变向量创建不可变向量?

如何将 Rust 中的树状 struct 展平为 Vec<&mut ...>?

为什么 i32 Box 类型可以在 Rust 中向下转换?

判断对象是 PyDatetime 还是 Pydate 的实例?

为什么 &i32 可以与 Rust 中的 &&i32 进行比较?

火箭整流罩、tokio-scheduler 和 cron 的生命周期问题

Rust:如果我知道只有一个实例,那么将可变borrow 转换为指针并返回(以安抚borrow 判断器)是否安全?