Rust 从基础到实践(17) 类型转型

rust_logo.jpg

类型转换在许多语言中都有,在 Rust 中类型转换很简单使用 as 后面跟着要转换为的类型。

fn main(){
    let f = 24.4321_f32;
    let i = f as u8;
    let c = i as char;

    println!("{} {} {} ", f, i, c);
    println!("{} {} ", 12 as char, 14 as char);
}
fn main(){
    let f = File::open("test.txt");

    let _f = match f {
        Ok(file) => file,
        Err(error) => {
            panic!("There was a problem opening the file: {:?}",error);
        },
    };
}

你可能感兴趣的:(Rust 从基础到实践(17) 类型转型)