Rust 程序设计语言 简体中文版 1.85.0
.expect("Index entered was not a number"); let element = a[index]; println!("The value of the element at index {index} is: {element}"); } 此代码编译成功。如果您使用 cargo run 运行此代码并输入 0、1、2、3 或 4,程序将在数组 文件名:src/main.rs fn main() { let a = [10, 20, 30, 40, 50]; for element in a { println!("the value is: {element}"); } } 示例 3-5:使用 for 循环遍历集合中的元素 当运行这段代码时,将看到与示例 3-4 一样的输出。更为重要的是,我们增强了代码安全性, &i32 = &v[2]; println!("The third element is {third}"); let third: Option<&i32> = v.get(2); match third { Some(third) => println!("The third element is {third}"), None => println0 码力 | 562 页 | 3.23 MB | 9 天前3
共 1 条
- 1