[Rust] Option Enum

pub enum Option2<T> {
    None,
    Some(T)
}

/**
impl is similar to typescript a class
with is_some method
*/
impl<T> Option2<T> {
    pub fn is_some(&self) -> bool {
        return match self {
            Option2::None => false
            Option2::Some(_) => true
        }
    }
}  

fn main () {
    let foo = Option2::Some(5);
    
    if foo.is_some() {
       let value = foo.unwrap();
    }
}

 

posted @ 2024-02-13 16:04  Zhentiw  阅读(3)  评论(0编辑  收藏  举报