第2章 原生类型

原生类型 — Rust By Example

译文 · 基于 Rust By Example

原文链接: https://doc.rust-lang.org/stable/rust-by-example/primitives.html

原生类型

Rust 提供了多种原生类型(primitives),包括:

标量类型(scalar type)

  • 有符号整数(signed integers):i8、i16、i32、i64、i128 和 isize(指针宽度)
  • 无符号整数(unsigned integers): u8、u16、u32、u64、u128 和 usize(指针宽度)
  • 浮点数(floating point): f32、f64
  • char(字符):单个 Unicode 字符,如 'a','α' 和 '∞'(每个都是 4 字节)
  • bool(布尔型):只能是 true 或 false
  • 单元类型(unit type):()。其唯一可能的值就是 () 这个空元组

尽管单元类型的值是个元组,它却并不被认为是复合类型,因为并不包含多个值。

复合类型(compound type)

  • 数组(array):如 [1, 2, 3]
  • 元组(tuple):如 (1, true)

变量都能够显式地给出类型说明(type annotation)。数字还可以通过后缀(suffix)或默认方式来声明类型。整型默认为 i32 类型,浮点型默认为 f64类型。注意 Rust 还可以根据上下文来推断(infer)类型(译注:比如一个未声明类型整数和 i64 的整数相加,则该整数会自动推断为 i64 类型。仅当根据环境无法推断时,才按默认方式取整型数值为 i32,浮点数值为 f64)。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    // 变量可以给出类型说明。
    let logical: bool = true;

    let a_float: f64 = 1.0;  // 常规说明
    let an_integer   = 5i32; // 后缀说明

    // 否则会按默认方式决定类型。
    let default_float   = 3.0; // `f64`
    let default_integer = 7;   // `i32`

    // 类型也可根据上下文自动推断。
    let mut inferred_type = 12; // 根据下一行的赋值推断为 i64 类型
    inferred_type = 4294967296i64;

    // 可变的(mutable)变量,其值可以改变。
    let mut mutable = 12; // Mutable `i32`
    mutable = 21;

    // 报错!变量的类型并不能改变。
    mutable = true;

    // 但可以用遮蔽(shadow)来覆盖前面的变量。
    let mutable = true;
}

参见:

std 库、mut、类型推断 和 变量遮蔽


01-字面量和运算符

字面量和运算符 — Rust By Example

02-元组

元组 — Rust By Example

03-数组和切片

数组和切片 — Rust By Example

最后修改 August 21, 2026: 更新 (76fc81a2e)