rust中的thiserror详细理解

复制代码
use anyhow::{bail, Context, Error, Result};
use std::{boxed, fs};
use std::io::Read;
use thiserror::Error;

// #[derive(Clone, Debug, Eq, Error, PartialEq)]
// #[error("Found no username in {0}")]
#[derive(Debug,Error)]
#[error("Found no username in {0}")]
struct EmptyUsernameError(String);

fn read_username(path: &str) ->Box<dyn std::error::Error>{

        return Box::new(EmptyUsernameError(path.to_string()));

}

fn main() {
    //fs::write("config.dat", "").unwrap();
    println!("{:?}", read_username("config.dat")) ;
}

结合https://docs.rs/thiserror/latest/thiserror/ 一起看。

#[derive(Debug,Error)] #[error("Found no username in {0}")]

这两个缺一不可

相关推荐
Fireworkitte6 小时前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
weixin-a153003083166 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
DCTANT6 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.7 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超7 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice7 小时前
对象的finalization机制Test
java·开发语言·jvm
思则变7 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
lijingguang7 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#
¥-oriented8 小时前
【C#中路径相关的概念】
开发语言·c#
CoderCodingNo8 小时前
【GESP】C++四级考试大纲知识点梳理, (7) 排序算法基本概念
开发语言·c++·排序算法