Skip to content

Commit

Permalink
feat: showcasing example of adding and holding a reference to a vector
Browse files Browse the repository at this point in the history
Signed-off-by: Fredrik Klingenberg <fredrkl@gmail.com>
  • Loading branch information
fredrkl committed Nov 19, 2023
1 parent 0b4caf0 commit c9bb868
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions projects/collections/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@ fn main() {
v.push(6);
v.push(7);
v.push(8);

let e = vec![1, 2, 3, 4, 5];
let third = &e[2];
// e.push(6); // Changing e to mutable (mut) and uncommenting this line will cause an error because we are trying to borrow a mutable reference to e while we have an immutable reference to e
println!("The third element is {}", third);

let third = e.get(2); // The reson why this is option is because it might not exist, the index might be out of bounds
match third {
Some(third) => println!("The third element is {}", third),
None => println!("There is no third element"),
}
}

0 comments on commit c9bb868

Please sign in to comment.