首页 > 其他分享 >[Rust] PartialEq in test

[Rust] PartialEq in test

时间:2024-03-01 16:23:14浏览次数:9  
标签:CreationError value PositiveNonzeroInteger test PartialEq Rust

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

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

impl PositiveNonzeroInteger {
    fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
        // Hmm... Why is this always returning an Ok value?
        if value == 0 {
            Err(CreationError::Zero)
        } else if (value < 0) {
            Err(CreationError::Negative)
        } else {
            Ok(PositiveNonzeroInteger(value as u64))
        }
    }
}

#[test]
fn test_creation() {
    assert!(PositiveNonzeroInteger::new(10).is_ok());
    assert_eq!(
        Err(CreationError::Negative),
        PositiveNonzeroInteger::new(-10)
    );
    assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0));
}

The PartialEq trait in Rust is used to enable equality comparisons (== and !=) on types. In the code snippet you've shared, PartialEq is derived for both the CreationError enum and the PositiveNonzeroInteger struct to allow for these equality comparisons, which are particularly useful and necessary in the context of testing.

Deriving PartialEq for CreationError and PositiveNonzeroInteger is necessary for enabling equality comparisons in tests, which is a common practice to check if the code behaves as expected, especially when dealing with error handling and custom types in Rust.

标签:CreationError,value,PositiveNonzeroInteger,test,PartialEq,Rust
From: https://www.cnblogs.com/Answer1215/p/18047346

相关文章

  • [Rust] .into()
    pubfngenerate_nametag_text(name:String)->Result<String,String>{ifname.is_empty(){//Emptynamesaren'tallowed.Err(String::from("`name`wasempty;itmustbenonempty."))}else{Ok(form......
  • 写给rust初学者的教程(一):枚举、特征、实现、模式匹配
    这系列RUST教程一共三篇。这是第一篇,介绍RUST语言的入门概念,主要有enum\trait\impl\match等语言层面的东西。安装好你的rust开发环境,用cargo创建一个空项目,咱们直接上代码。懵逼的同僚可以参考我8年前的rust文章:https://www.iteye.com/blog/somefuture-2275494,虽然8年了,然并不......
  • Rust的Display和ToString:将对象转换为字符串
    在写代码的时候,我们经常需要将对象输出到屏幕上,或者转换为字符串;在Python中,我们可以为类型定义魔法函数__str__,print和str()都会调用它;在C++中,我们可以为对象重载ostream&operator<<(ostream&os)函数,使用ostringstream、fstream和cout的时候会调用它。在Rust中该实现什么,想必大......
  • Rust的From和Into特征:可能是最常用的转换类特征
    说到From和Into,以及从他们中衍生出的TryFrom和TryInto,想必大家都不会陌生。它们不像Borrow、AsRef、ToOwned这些默默工作在泛型里的特征,是绝大多数Rust开发者每天都会使用到的东西。今天我们就来加深一下对这四个特征的了解吧~From和Into如果说AsRef和AsMut的功能是做“引用到引......
  • Go 100 mistakes - #86: Sleeping in unit tests
      ......
  • AtCoder Regular Contest 172
    Preface开学了小溜一下之前没打的ARC,结果这场后面没有计数改成数论了又给我创飞了这场的DE都太玄学了,属于是自己想半天一点屌思路没有然后看一眼题解就顿悟的类型总结就是菜得发昏A-Chocolate挺有意思的签到题考虑从大到小依次切,对于一个原来\(H'\timesW'\)的块,为了尽量......
  • Go 100 mistakes - #82: Not categorizing tests
         ......
  • 基于FPGA的9/7整数小波变换和逆变换verilog实现,包含testbench
    1.算法运行效果图预览 将测试结果导入到matlab显示   2.算法运行软件版本vivado2019.2,matlab2022a 3.算法理论概述      小波变换是一种在信号处理中广泛应用的数学工具,它能够提供信号在不同尺度和位置上的信息。在图像处理、数据压缩、噪声消除等领域,小......
  • Rust 交叉编译 macOS 为 Linux 和 Windows
    目录前言环境案例macOS编译为Linux和Windows可用二进制程序编译为Linux平台编译为Windows平台最后前言鉴于rust中文资料较少,遇到问题的解决方案更少。这里记录遇到的一些问题。Rust支持交叉编译,可以在macOS平台编译出Linux或者Windows可运行的程序,或者在Lin......
  • 抖音技术分享:飞鸽IM桌面端基于Rust语言进行重构的技术选型和实践总结
    本文由ELab团队公众号授权发布,原题《Rust语言在IM客户端的实践》,来自抖音电商前端团队的分享,本文有修订和改动。1、引言本文将介绍飞鸽IM前端团队如何结合Rust对飞鸽客户端接待能力进行的技术提升,一步步从概念验证、路径分解到分工开发,再到最后上线收益论证,并分享了其中遇到的......