rust多样化错误处理(从零学习)

 更新时间:2023年11月10日 09:38:56   作者:-咕咚  
一个优秀的项目,错误处理的优雅性是至关重要的,而rust,anyhow creat是绕不过去的一个,今天我们来研究下,怎么使用它,帮助我们写出更优雅的代码,有需要的朋友可以借鉴参考下,希望能够有所帮助

一、thiserror初体验

可以使用命令 cargo add thiserror 将它添加到自己的项目中,或者在 Cargo.toml 中添加如下的配置:

[dependencies]
thiserror = "1.0"

thiserror 可以用于枚举或者结构体,例如,我们来看一个基本的例子:

use std::io;
use log::error;
use thiserror::Error;
#[derive(Error,Debug)]
pub enum DataStoreError {
    #[error("data store disconnected")]
    Disconnect(#[from] std::io::Error),
    #[error("the data for key `{0}` is not available")]
    Redaction(String),
    #[error("invalid header (expected {expected:?},found {found:?})")]
    InvalidHeader {expected:String,found:String},
    #[error("unknown data store error")]
    Unknown
}

pub fn error(){
    ///error
    println!("这是没有参数的 Unknown {}",DataStoreError::Unknown);
    println!("这是结构体参数的 InvalidHeader {}",DataStoreError::InvalidHeader {
        expected : String::from("expected"),
        found : String::from("found")
    });
    println!("这是有index参数的 Redaction {}",DataStoreError::Redaction(String::from("Redaction")));
    println!("这是有from参数的 Disconnect {}",DataStoreError::Disconnect(io::Error::from(io::ErrorKind::TimedOut)));

}

这是没有参数的 Unknown unknown data store error
这是结构体参数的 InvalidHeader invalid header (expected "expected",found "found")
这是有index参数的 Redaction the data for key `Redaction` is not available
这是有from参数的 Disconnect data store disconnected

然后我们来仔细分析下各种用法

二、#[error]

如果使用 #[error(...)] 为结构体或者枚举生成自定义错误消息,这将为它们实现 Display

2.1 Enum

#[derive(Debug)]
pub struct Limits{
    lo : i16,
    hi : i16
}
#[derive(Error,Debug)]
pub enum MyError{
    #[error("invalid rdo_lookahead_frames {0} (expected < {})",i8::MAX)]
    InvalidLookahead(u32),
    #[error("first letter must be lowercase but was {:?}",first_char(.0))]
    WrongCase(String),
    #[error("invalid index {idx},expected at least {} and at most {}",.limits.lo,.limits.hi)]
    OutOfBounds{idx:usize,limits:Limits}
}
pub fn error(){
    println!("这是 enum 的InvalidLookahead {}",MyError::InvalidLookahead(3333));
    //自动调用函数进行比较
    println!("这是 enum 的 WrongCase {}",MyError::WrongCase("kk".to_string()));
    println!("这是 enum 的 OutOfBounds {}",MyError::OutOfBounds{idx : 89,limits:Limits{
        lo:12,
        hi:11
    }});

}

这是 enum 的InvalidLookahead invalid rdo_lookahead_frames 3333 (expected < 127)
这是 enum 的 WrongCase first letter must be lowercase but was 'k'
这是 enum 的 OutOfBounds invalid index 89,expected at least 12 and at most 11

2.2 struct

#[derive(Error, Debug)]
#[error("something failed, msg is: {msg}")]
pub struct MyErrorStruct {
    msg: &'static str,
}
println!("这是 struct 的msg  {}",MyErrorStruct{msg:"失败的msg"});

这是 struct 的msg  something failed, msg is: 失败的msg

2.3 其他结构

其他结构也是支持的,例如 tuple、空struct 等等

三、#[from]

可以使用 #[from] 注解为错误类型实现 From,可以从其他错误生成:

#[derive(Error, Debug)]
#[error("some io error happened, {:?}", .source)]
pub struct MyFromError {
    #[from]
    source: io::Error,
}
println!("这是 struct 的 from 的 {}",MyFromError::from(io::Error::from(io::ErrorKind::TimedOut)));

这是 struct 的 from 的 some io error happened, Kind(TimedOut)

四、#[source]

可以使用 #[source] 属性,或者将字段命名为 source,可为自定义错误实现 source 方法,返回底层的错误类型:

use std::error::Error;
use std::io;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum MyError {
  #[error("some io error happened, {:?}", .source)]
  IO { source: io::Error },
}
fn main() {
  let err = MyError::IO {
      source: io::Error::from(io::ErrorKind::TimedOut),
  };
  println!("{:?}", err.source());
}

或者使用 #[source] 属性标记非 source 的字段,例如:这里是 err 字段:

use std::error::Error;
use std::io;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum MyError {
  #[error("some io error happened, {:?}", .err)]
  IO {
      #[source]
      err: io::Error,
  },
}

fn main() {
  let err = MyError::IO {
      err: io::Error::from(io::ErrorKind::TimedOut),
  };
  println!("{:?}", err.source());
}

#[from] 和 #[source] 二选一即可,#[from] 也会为类型生成 .source() 方法,例如:

#![allow(unused)]
#![feature(backtrace)]
use std::backtrace;
use std::error::Error as _;
use std::io;
use thiserror::Error;
#[derive(Error, Debug)]
#[error("some io error happened, {:?}", .source)]
pub struct MyError {
  #[from]
  source: io::Error,
  backtrace: backtrace::Backtrace,
}
fn main() {
  let err = MyError::from(io::Error::from(io::ErrorKind::TimedOut));
  println!("{:?}", err.source());
}

五、#[backtrace]

只要在我们的错误结构体里面放个类型为 std::backtrace::Backtrace 的字段,就会自动实现 backtrace() 方法,可以看 #[from]

另外,如果使用 #[backtrace] 标记 sourcesource 字段,或者 #[source],或者 #[from]),那么 backtrace() 方法会转发到 source 的 backtrace

六、#[error(transparent)]

可以通过 #[error(transparent)] 让 source 和 Display 直接使用底层的错误,这对于那些想处理任何的类型来说是很有用的:

use std::io;
use log::error;
use thiserror::Error;
use anyhow::anyhow;
use std::error::Error as _;
#[derive(Error,
#[derive(Error, Debug)]
#[error(transparent)]
pub struct MyErrorTrans {
    #[from]
    source: anyhow::Error,
}
#[derive(Error, Debug)]
pub enum MyErrorTransEnum {
    #[error("file not found")]
    FileNotFound,
    #[error(transparent)]
    Other(#[from] anyhow::Error), // source and Display delegate to anyhow::Error
}
//transparent
let err = MyErrorTrans::from(anyhow!("Missing attribute: {}", "field1"));
println!("{}", err);
println!("{:?}", err);

let err = MyErrorTransEnum::from(anyhow!("Missing attribute: {}", "field1"));
println!("{}", err);
println!("{:?}", err);

Missing attribute: field1
MyErrorTrans { source: Missing attribute: field1 }
Missing attribute: field1
Other(Missing attribute: field1)

以上就是rust多样化错误处理(从零学习)的详细内容,更多关于rust多样化错误处理的资料请关注脚本之家其它相关文章!

相关文章

  • 探索Rust切片与Go有何区别

    探索Rust切片与Go有何区别

    这篇文章主要为大家介绍了Rust切片与Go的区别探索,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • rust 中生成与使用protobuf的方法

    rust 中生成与使用protobuf的方法

    这篇文章主要介绍了rust中protobuf生成与使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • Rust语言从入门到精通系列之Iterator迭代器深入详解

    Rust语言从入门到精通系列之Iterator迭代器深入详解

    这篇文章主要为大家介绍了Rust语言从入门到精通系列之Iterator迭代器深入详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • Rust中的Vector多值存储使用方法

    Rust中的Vector多值存储使用方法

    Vector在Rust中是一个非常灵活和强大的数据结构,通过有效利用它,我们可以更加方便地处理和操作多个值,使得代码更加清晰和易于维护,这篇文章主要介绍了Rust中的Vector多值存储的利器,需要的朋友可以参考下
    2024-02-02
  • 使用systemd部署r-nacos的操作方法

    使用systemd部署r-nacos的操作方法

    r-nacos是一个用rust实现的nacos服务,我们用它平替java nacos以降低服务占用内存,提升服务的稳定性,这篇文章主要介绍了使用systemd部署r-nacos,需要的朋友可以参考下
    2024-03-03
  • Rust中Protobuf使用详解

    Rust中Protobuf使用详解

    本文介绍了如何在Rust中使用Protobuf进行序列化和反序列化,并介绍了如何在项目中使用生成的Rust代码,通过include!宏和mod.rs文件的配置,可以方便地在Rust项目中使用Protobuf,感兴趣的朋友跟随小编一起看看吧
    2025-11-11
  • Rust 中判断两个 HashMap 是否相等

    Rust 中判断两个 HashMap 是否相等

    在Rust标准库中,HashMap 实现了 PartialEq 和 Eq trait,但是这些trait的实现是基于严格的结构相等性,包括元素的顺序,这篇文章主要介绍了Rust 中判断两个 HashMap 是否相等,需要的朋友可以参考下
    2024-04-04
  • Rust中的&和ref使用解读

    Rust中的&和ref使用解读

    在Rust中,`&`和`ref`都可以用来定义指针,但它们的使用位置不同,`&`通常放在等号右边,而`ref`放在左边,`&`主要用于函数参数和模式匹配中,而`ref`主要用于模式匹配中,Rust通过`&`和`ref`提供了灵活的指针操作,使得代码更加安全和高效
    2025-02-02
  • 详解Rust中的workspace

    详解Rust中的workspace

    这篇文章主要向大家介绍Rust中的workspace,主要内容包括基础应用、实用技巧、原理机制等方面,这个概念在Rust中是通用的,只不过maven换成了cargo,而模块变成了crate,下面跟着小编通过一个例子给大家介绍下
    2022-03-03
  • Rust使用Sled添加高性能嵌入式数据库

    Rust使用Sled添加高性能嵌入式数据库

    这篇文章主要为大家详细介绍了如何在Rust项目中使用Sled库,一个为Rust生态设计的现代、高性能嵌入式数据库,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03

最新评论