Common collections
vector
Doc:https://doc.rust-lang.org/std/vec/struct.Vec.html
more example:https://doc.rust-lang.org/stable/rust-by-example/std/vec.html
rust
fn vec_func() {
// Creating
let mut v: Vec<i32> = Vec::new(); // new
let mut v = vec![1, 2, 3, 4]; // mocro
// update
v.push(10);
v.push(20);
v.push(30);
v.push(40);
// get element
let v = vec![1, 2, 3, 4, 5];
let third: &i32 = &v[2];
println!("The third element is {}", third);
match v.get(2) {
Some(third) => println!("The third element is {}", third),
None => println!("There is no third element."),
}
// iterating
let mut v = vec![100, 32, 57];
for i in &v { // unmutable
println!("{}", i);
}
for i in &mut v { // mutable
*i += 50;
}
for (i, x) in v.iter().enumerate() { // with index
println!("In position {} we have value {}", i, x);
}
// with enum
enum SpreadsheetCell {
Int(i32),
Float(f64),
Text(String),
}
let row = vec![
SpreadsheetCell::Int(3),
SpreadsheetCell::Text(String::from("blue")),
SpreadsheetCell::Float(10.12),
];
}