Creating vector in rust.
let mut v = Vec::new();
v.push(5);
v.push(6);
v.push(7);
v.push(8);
println!("{:?}", v)
[5, 6, 7, 8]
Reading Elements of Vectors
let v = vec![1, 2, 3, 4, 5];
let third: &i32 = &v[2];
println!("The third element is {third}");
let third: Option<&i32> = v.get(2);
match third {
Some(third) => println!("The third element is {third}"),
None => println!("There is no third element."),
}
println!("{:?}", v)
The third element is 3
The third element is 3
[1, 2, 3, 4, 5]
Option<T> is an enum that represents the presence or absence of a value. It is a fundamental type in Rust’s approach to handling nullable values and potential errors, promoting explicit handling rather than implicit null pointers found in other languages.