rust eq

python;gutter:true; use std::cmp::Ordering;</p> <h1>[derive(Debug)]</h1> <p>enum D{ F(f64) }</p> <p>fn getnum(s:&D) ->f64{ if let D::F(x) = s{ *x }else{ panic!("no value"); } }</p> <p>impl std::cmp::PartialEq for D { fn eq(&self, other: &Self) -> bool { let a = getnum(self); let b = getnum(other); if a == b { true }else{ false } } }</p> <p>impl std::cmp::PartialOrd for D { fn partial_cmp(&self, other: &Self) -> Option { let a = getnum(self); let b = getnum(other); if a> b{ Some(Ordering::Greater) }else if a == b { Some(Ordering::Equal) }else{ Some(Ordering::Less) } } }</p> <p>impl std::cmp::PartialEq for D { fn eq(&self, other: &i32) -> bool { let a = getnum(self); let b = *other as f64; if a == b { true }else{ false } } }</p> <p>impl std::cmp::PartialOrd for D { fn partial_cmp(&self, other: &i32) -> Option { let a = getnum(self); let b = *other as f64; if a> b{ Some(Ordering::Greater) }else if a == b { Some(Ordering::Equal) }else{ Some(Ordering::Less) } } }</p> <p>fn main(){ let a = D::F(1.4); let b = 1_i32;</p> <pre><code>println!("{:?}", a>b); // if a > b { // println!("yes"); // }else if a == b{ // println!("equal"); // } else{ // println!("less"); // } </code></pre> <p>}

Original: https://www.cnblogs.com/pythonClub/p/16552259.html
Author: CrossPython
Title: rust eq

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15766/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部