]> git.proxmox.com Git - rustc.git/blame - src/doc/book/listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access/src/main.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / src / doc / book / listings / ch03-common-programming-concepts / no-listing-15-invalid-array-access / src / main.rs
CommitLineData
5869c6ff
XL
1use std::io;
2
74b04a01
XL
3fn main() {
4 let a = [1, 2, 3, 4, 5];
5869c6ff
XL
5
6 println!("Please enter an array index.");
7
8 let mut index = String::new();
9
10 io::stdin()
11 .read_line(&mut index)
12 .expect("Failed to read line");
13
14 let index: usize = index
15 .trim()
16 .parse()
17 .expect("Index entered was not a number");
74b04a01
XL
18
19 let element = a[index];
20
04454e1e 21 println!("The value of the element at index {index} is: {element}");
74b04a01 22}