list structure: 存储在heap中;
Vec<T>
创建对象:let v: Vec<i32> = Vec::new();
推断Vec类型:vec!宏;
let v = vec![1, 2, 3];
更新一个vector:
let mut v = Vec::new();
v.push(5);
v.push(6);
v.push(7);
v.push(8);
当vector被dropped,所有的元素也dropped;
vector读取元素:
let v = vec![1,2,3,4];
let third: &i32 = &vec[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");
}
vector迭代:
let v = vec![100, 32, 57];
for i in &v {
println!("{}", i);
}
使用枚举存储不同类型的数据:
enum SreadsheetCell {
Int(i32),
Float(f64),
Text(String),
}
let row = vec![
SpreadsheetCell::Int(3),
SpreadsheetCell::Float(10.13),
SpreadsheetCell::Text(String::from("blue")),
];
rust --vector 学习
最新推荐文章于 2024-12-05 23:04:46 发布