Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Built-in Rust types
Rust 内建类型

What you’ll learn: Rust’s fundamental types (i32, u64, f64, bool, char), type inference, explicit type annotations, and how they compare to C/C++ primitive types. No implicit conversions — Rust requires explicit casts.
本章将学到什么: Rust 的基础类型,例如 i32u64f64boolchar,以及类型推断、显式类型标注,还有它们和 C/C++ 基本类型的对照关系。Rust 没有隐式类型转换,涉及转换时必须显式 cast。

  • Rust has type inference, but also allows explicit specification of the type
    Rust 支持类型推断,同时也允许显式写出类型。
DescriptionTypeExample
Signed integers
有符号整数
i8, i16, i32, i64, i128, isize
i8、i16、i32、i64、i128、isize
-1, 42, 1_00_000, 1_00_000i64
-1、42、1_00_000、1_00_000i64
Unsigned integers
无符号整数
u8, u16, u32, u64, u128, usize
u8、u16、u32、u64、u128、usize
0, 42, 42u32, 42u64
0、42、42u32、42u64
Floating point
浮点数
f32, f64
f32、f64
0.0, 0.42
0.0、0.42
Unicode
Unicode 字符
char
char
‘a’, ‘$’
'a''$'
Boolean
布尔值
bool
bool
true, false
true、false
  • Rust permits arbitrarily use of _ between numbers for ease of reading
    Rust 允许在数字中任意插入 _ 来增强可读性。

Rust type specification and assignment
Rust 类型标注与赋值

  • Rust uses the let keyword to assign values to variables. The type of the variable can be optionally specified after a :
    Rust 使用 let 给变量赋值。变量类型可以省略,也可以在 : 后面显式标出。
fn main() {
    let x : i32 = 42;
    // These two assignments are logically equivalent
    let y : u32 = 42;
    let z = 42u32;
}
  • Function parameters and return values (if any) require an explicit type. The following takes an u8 parameter and returns u32
    函数参数和返回值如果存在,都必须显式标注类型。下面这个函数接收一个 u8 参数,并返回 u32
#![allow(unused)]
fn main() {
fn foo(x : u8) -> u32
{
    return x as u32 * x as u32;
}
}
  • Unused variables are prefixed with _ to avoid compiler warnings
    未使用变量通常以前缀 _ 命名,这样可以避免编译器警告。

Rust type specification and inference
Rust 类型标注与类型推断

fn secret_of_life_u32(x : u32) {
    println!("The u32 secret_of_life is {}", x);
}

fn secret_of_life_u8(x : u8) {
    println!("The u8 secret_of_life is {}", x);
}

fn main() {
    let a = 42; // The let keyword assigns a value; type of a is u32
    let b = 42; // The let keyword assigns a value; inferred type of b is u8
    secret_of_life_u32(a);
    secret_of_life_u8(b);
}

Rust variables and mutability
Rust 变量与可变性

  • Rust variables are immutable by default unless the mut keyword is used to denote that a variable is mutable. For example, the following code will not compile unless the let a = 42 is changed to let mut a = 42
    Rust 变量默认是 不可变 的,除非显式使用 mut 表示该变量可变。比如下面这段代码,如果不把 let a = 42 改成 let mut a = 42,就无法通过编译。
fn main() {
    let a = 42; // Must be changed to let mut a = 42 to permit the assignment below 
    a = 43;  // Will not compile unless the above is changed
}
  • Rust permits the reuse of the variable names (shadowing)
    Rust 允许重复使用变量名,这叫 shadowing。
fn main() {
    let a = 42;
    {
        let a = 43; //OK: Different variable with the same name
    }
    // a = 43; // Not permitted
    let a = 43; // Ok: New variable and assignment
}