标签: thiserror

重用“thiserror”定义中的错误消息

我使用这个错误箱在我的项目中进行错误处理。

我声明一个这样的错误

#[derive(Debug, thiserror::Error)]
enum CustomErrors {
    #[error("This is custom error one")]
    CustomErrorOne,

    #[error("This is custom error two")]
    CustomErrorTwo
}
Run Code Online (Sandbox Code Playgroud)

我像这样使用这个自定义错误

// cut

match foo() {
  Err(errors) -> match errors {
     CustomErrors::CustomErrorOne => ..., // I want to get access to "This is custom error one" error message here
     CustomErrors::CustomErrorTwo => ..., // ...and here
  }
}

//cut
Run Code Online (Sandbox Code Playgroud)

我是否正确理解,由于哲学的原因,这是不可能的thiserror?并且需要创建新的错误消息?

此错误故意不出现在您的公共 API 中。(c)文件

rust thiserror

3
推荐指数
1
解决办法
1422
查看次数

此错误是否会带来不稳定的功能,使其无法与稳定的 Rust 版本一起使用?

我正在研究这个回购协议。它在 M2 笔记本电脑上的 MacOS 上构建没有问题;但是当我尝试在 Debian 桌面上构建它时,它显示以下错误:

error[E0554]: `#![feature]` may not be used on the stable release channel
   --> /home/username/.cargo/registry/src/github.com-1ecc6299db9ec823/thiserror-1.0.37/src/lib.rs:238:34
    |
238 | #![cfg_attr(provide_any, feature(provide_any))]
    |                                  ^^^^^^^^^^^

For more information about this error, try `rustc --explain E0554`.
error: could not compile `thiserror` due to previous error
Run Code Online (Sandbox Code Playgroud)

所以我尝试在 Debian 上使用 nightly build rustup default nightly,然后它就没有错误了。

我很困惑,因为这个错误是一个很受欢迎的错误。即使这个错误箱不能与稳定的 Rust 一起工作,但它确实具有不稳定的功能吗?这是否意味着大多数 Rust 开发人员使用夜间版本而不是稳定版本?

但为什么它在 Mac 上对我有效呢?我很确定我在 Mac 上有稳定版本。

一定有什么我错过了:) 提前谢谢你!

rust thiserror

3
推荐指数
1
解决办法
866
查看次数

标签 统计

rust ×2

thiserror ×2