Rust教學
Rust下載和安裝
Rust簡介
Rust Hello World
Rust的特點
Rust註釋
Rust開發環境安裝
Rust文檔
Rust第一個程序
Rust格式化打印
Rust調試
Rust顯示
測試用例:列表
Rust格式化
Rust原語
Rust常量和運算符
Rust元組
Rust數組和切片
Rust自定義類型
Rust結構
Rust可視性
Rust枚舉
Rust常量
Rust變量綁定
Rust變量綁定-可變性
Rust變量綁定-範圍和陰影
Rust變量綁定-聲明
Rust類型轉換
Rust類型轉換 - 字面量
Rust類型轉換-推導
Rust類型轉換 - 別名
Rust表達式
Rust if/else語句
Rust循環
Rust嵌套和標籤
Rust while循環
Rust for和範圍
Rust匹配/match
Rust匹配析構元組
Rust解構枚舉
Rust指針和引用
Rust解構結構
Rust Guards
Rust綁定
Rust if let
Rust while let
Rust函數
Rust方法
Rust閉包
Rust捕捉
Rust作爲輸入參數
Rust匿名類型
Rust輸入函數
Rust作爲輸出參數
Rust類型轉換
Rust類型轉換
Rust提供原始類型到隱式類型之間的轉換(強制)。但是,顯式類型轉換可以使用關鍵字as進行。
整數類型之間的轉換規則遵循C約定, 除了在C是未定義行爲的轉換。所有類型轉換整型的行爲在Rust都很好的定義。
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);
// when casting any value to an unsigned type, T,
// std::T::MAX + 1 is added or subtracted until the value
// fits into the new type
// 1000 already fits in a u16
println!("1000 as a u16 is: {}", 1000 as u16);
// 1000 - 256 - 256 - 256 = 232
println!("1000 as a u8 is : {}", 1000 as u8);
// -1 + 256 = 255
println!(" -1 as a u8 is : {}", (-1i8) as u8);
// For positive numbers, this is the same as the modulus
println!("1000 mod 256 is : {}", 1000 % 256);
// When casting to a signed type, the result is the same as
// first casting to the corresponding unsigned type then
// taking the two's complement.
// Unless it already fits, of course.
println!(" 128 as a i16 is: {}", 128 as i16);
// 128 as u8 -> 128, whose two's complement in eight bits is:
println!(" 128 as a i8 is : {}", 128 as i8);
// repeating the example above
// 1000 as u8 -> 232
println!("1000 as a i8 is : {}", 1000 as i8);
// and the two's complement of 232 is -24
println!(" 232 as a i8 is : {}", 232 as i8);
}