通过例子学习Rust

5.1 转换

Rust不提供基础类型之间的隐式类型转换,只能使用 as 关键字显式转换类型。

fn main() { let decimal = 65.4321_f32; // Error! No implicit conversion let integer: u8 = decimal; // FIXME ^ Comment out this line // Explicit conversion let integer = decimal as u8; let character = integer as char; println!("Casting: {} -> {} -> {}", decimal, integer, character); }