Refining Ch03.

This commit is contained in:
rust-lang.xfoss.com
2023-12-08 16:54:21 +08:00
parent 33161694d1
commit f768d5f9c3
2 changed files with 80 additions and 30 deletions

View File

@@ -1,5 +1,22 @@
fn main() {
let a: [i32; 5] = [-1, 0, 1, 2, 3];
use std::io;
println! ("a[0]: {a.0}");
fn main() {
let a = [1, 2, 3, 4, 5];
println! ("请输入一个数组索引。");
let mut index = String::new();
io::stdin()
.read_line(&mut index)
.expect("读取行失败failed to read line");
let index: usize = index
.trim()
.parse()
.expect("输入的所以并非一个数字");
let element = a[index];
println! ("位于索引 {index} 出的元素值为:{element}");
}