aiLatte aiLatte contact@ailatte.com

Rust枚举获取值

» rust
use std::fmt;

enum MyCell {
    Int(i32),
    Float(f64),
    Text(String),
}

impl fmt::Display for MyCell {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            MyCell::Int(i) => write!(f, "{}", i),
            MyCell::Float(fl) => write!(f, "{}", fl),
            MyCell::Text(t) => write!(f, "{}", t),
        }
    }
}

fn main() {
    let a = MyCell::Int(5);
    let b = MyCell::Float(3.14);
    let c = MyCell::Text(String::from("ABC"));
    println!("{}, {}, {}", a, b, c);
}