< 返回版块

Rust怎么输出枚举的变量的值

九世 发表于

fn main(){ enum Sprean{ Int(i32), Float(f64), Text(String), }

let row=vec![
    Sprean::Int(3),
    Sprean::Float(10.12),
    Sprean::Text(String::from("blue")),
];

println!("This is your {}",row[0]);

}

评论区

123hc 2018-07-26T11:02:16.943307
enum Sprean{ 
    Int(i32),
    Float(f64), 
    Text(String), 
}
fn main(){ 

let row=vec![
    Sprean::Int(3),
    Sprean::Float(10.12),
    Sprean::Text(String::from("blue")),
];

if let Sprean::Int(x) =row[0]{
        println!("This is your {}",x);
    }
}
作者 九世 2018-07-27T05:00:04.626837

Thanks♪(・ω・)ノ

@123hc enum Sprean{ Int(i32), Float(f64), Text(String), } fn main(){

let row=vec![ Sprean::Int(3), Sprean::Float(10.12), Sprean::Text(String::from("blue")), ];

if let Sprean::Int(x) =row[0]{ println!("This is your {}",x); } }

123hc 2018-07-27T15:22:22.663782

还有可以使用match

@九世 Thanks♪(・ω・)ノ

@123hc enum Sprean{ Int(i32), Float(f64), Text(String), } fn main(){ let row=vec![ Sprean::Int(3), Sprean::Float(10.12), Sprean::Text(String::from("blue")), ]; if let Sprean::Int(x) =row[0]{ println!("This is your {}",x); } }

idle 2018-08-13T17:40:08.977677

也可以用这种方法

#[derive(Debug)]
enum Sprean {
    Int(i32),
    Float(f64),
    Text(String),
}
fn main() {
    let row = vec![
        Sprean::Int(3),
        Sprean::Float(10.12),
        Sprean::Text(String::from("blue")),
    ];

    println!("This is your {:?}", row[1]);
}
1 共 4 评论, 共 1 页