trpl-zh-cn/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs

23 lines
445 B
Rust
Raw Normal View History

2022-02-06 16:43:51 +08:00
use std::io;
fn main() {
let a = [1, 2, 3, 4, 5];
println!("Please enter an array index.");
let mut index = String::new();
io::stdin()
.read_line(&mut index)
.expect("Failed to read line");
let index: usize = index
.trim()
.parse()
.expect("Index entered was not a number");
let element = a[index];
println!("The value of the element at index {index} is: {element}");
2022-02-06 16:43:51 +08:00
}