rust eq

use std::cmp::Ordering;

#[derive(Debug)]
enum D{
    F(f64)
}

fn getnum(s:&D) ->f64{
    if let D::F(x) = s{
        *x
    }else{
        panic!("no value");
    }
}

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 }
    }
}

impl std::cmp::PartialOrd for D {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        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)
        }
    }
}

impl std::cmp::PartialEq<i32> for D {
    fn eq(&self, other: &i32) -> bool {
        let a = getnum(self);
        let b = *other as f64;
        if a == b { 
            true
        }else{
            false
        }
    }
}

impl std::cmp::PartialOrd<i32> for D {
    fn partial_cmp(&self, other: &i32) -> Option<Ordering> {
        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)
        }
    }
}

fn main(){
    let a = D::F(1.4);
    let b = 1_i32;

    println!("{:?}", a>b);
    // if a > b { 
    //     println!("yes");
    //  }else if a == b{ 
    //     println!("equal");
    //   } else{
    //     println!("less");
    //   }
}

  

posted @ 2022-08-04 20:34  CrossPython  阅读(23)  评论(0编辑  收藏  举报