[rustlings]13_error_handling

errors6
这一个就是在Err(E)中加了点手脚,就是Err(E)中E的类型也是一个Err类型.
这里是创建了一个新的Err类型,Err类型中有两种不同的枚举值.对于不同的枚举值代表两种不同的错误.

// Using catch-all error types like `Box<dyn Error>` isn't recommended for
// library code where callers might want to make decisions based on the error
// content instead of printing it out or propagating it further. Here, we define
// a custom error type to make it possible for callers to decide what to do next
// when our function returns an error.

use std::num::ParseIntError;

#[derive(PartialEq, Debug)]
enum CreationError {
    Negative,
    Zero,
}

// A custom error type that we will be using in `PositiveNonzeroInteger::parse`.
#[derive(PartialEq, Debug)]
enum ParsePosNonzeroError {
    Creation(CreationError),
    ParseInt(ParseIntError),
}

impl ParsePosNonzeroError {
    fn from_creation(err: CreationError) -> Self {
        Self::Creation(err)
    }

    // TODO: Add another error conversion function here.
    // fn from_parseint(???) -> Self { ??? }
    fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError{
        ParsePosNonzeroError::ParseInt(err)
    }
}

#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);

impl PositiveNonzeroInteger {
    fn new(value: i64) -> Result<Self, CreationError> {
        match value {
            x if x < 0 => Err(CreationError::Negative),
            0 => Err(CreationError::Zero),
            x => Ok(Self(x as u64)),
        }
    }

    fn parse(s: &str) -> Result<Self, ParsePosNonzeroError> {
        // TODO: change this to return an appropriate error instead of panicking
        // when `parse()` returns an error.
        let x: i64 = match s.parse(){
            Ok(t) => t,
            Err(e) => {
                return Err(ParsePosNonzeroError::from_parseint(e));
            },
        };
        Self::new(x).map_err(ParsePosNonzeroError::from_creation)
    }
}

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_parse_error() {
        assert!(matches!(
            PositiveNonzeroInteger::parse("not a number"),
            Err(ParsePosNonzeroError::ParseInt(_)),
        ));
    }

    #[test]
    fn test_negative() {
        assert_eq!(
            PositiveNonzeroInteger::parse("-555"),
            Err(ParsePosNonzeroError::Creation(CreationError::Negative)),
        );
    }

    #[test]
    fn test_zero() {
        assert_eq!(
            PositiveNonzeroInteger::parse("0"),
            Err(ParsePosNonzeroError::Creation(CreationError::Zero)),
        );
    }

    #[test]
    fn test_positive() {
        let x = PositiveNonzeroInteger::new(42).unwrap();
        assert_eq!(x.0, 42);
        assert_eq!(PositiveNonzeroInteger::parse("42"), Ok(x));
    }
}

相关推荐

  1. [rustlings]13_error_handling

    2024-07-20 23:52:03       20 阅读
  2. Vue3 onErrorCaptured errorHandler 异常处理

    2024-07-20 23:52:03       31 阅读
  3. [rustlings]11_hashmaps

    2024-07-20 23:52:03       19 阅读
  4. Github 2024-07-13 Rust开源项目日报 Top10

    2024-07-20 23:52:03       23 阅读
  5. Rust安装——Win10

    2024-07-20 23:52:03       48 阅读
  6. Rust-10-数据类型

    2024-07-20 23:52:03       20 阅读
  7. rust学习笔记(8-12

    2024-07-20 23:52:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 23:52:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 23:52:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 23:52:03       45 阅读
  4. Python语言-面向对象

    2024-07-20 23:52:03       55 阅读

热门阅读

  1. C语言经典例题-5

    2024-07-20 23:52:03       22 阅读
  2. 【面试题】Golang 锁的相关问题(第七篇)

    2024-07-20 23:52:03       17 阅读
  3. Perl编程艺术:探索代码重用的无限可能

    2024-07-20 23:52:03       12 阅读
  4. Python 基础——列表(list)

    2024-07-20 23:52:03       17 阅读
  5. jvm-证明cpu指令是乱序执行的案例

    2024-07-20 23:52:03       21 阅读