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

数组类型

Syntax
ArrayType[ Type ; Expression ]

数组是类型为 TN 个元素的固定大小序列。数组类型写为 [T; N]

大小是求值为 usize常量表达式

示例:

#![allow(unused)]
fn main() {
// A stack-allocated array
let array: [i32; 3] = [1, 2, 3];

// A heap-allocated array, coerced to a slice
let boxed_array: Box<[i32]> = Box::new([1, 2, 3]);
}

数组的所有元素始终是已初始化的,并且在安全方法和运算符中对数组的访问始终进行边界检查。

Note

Vec<T> 标准库类型提供堆分配的可调整大小的数组类型。