Skip to content

Commit 897ab68

Browse files
Working with vectors.
1 parent 0b6bafb commit 897ab68

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "vectors"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fn main() {
2+
let mut marks:Vec<u32> = vec![10, 20, 30, 40, 50];
3+
4+
// Accessing elements of a vector directly using the index. Using an index out of
5+
// bounds will cause a panic.
6+
let top = &marks[4];
7+
println!("The top mark is {}", top);
8+
9+
// Accessing elements of a vector using the get() method. Using an index out of
10+
// bounds will return None.
11+
let bottom = marks.get(0);
12+
match bottom {
13+
Some(mark) => println!("The bottom mark is {}", mark),
14+
None => println!("No bottom mark found"),
15+
}
16+
17+
println!("Adding 10 to each mark");
18+
19+
// Iterating over mutable references to each element in the vector.
20+
for mark in &mut marks {
21+
*mark += 10;
22+
}
23+
24+
// Iterating over immutable references to each element in the vector.
25+
for mark in &marks {
26+
println!("Mark: {}", mark);
27+
}
28+
}

0 commit comments

Comments
 (0)