走进 Rust:Vector 集合
Rust About 695 wordsVector
let v: Vec<i32> = Vec::new();let v = vec![1, 2, 3, 4, 5];添加元素
let mut v = Vec::new();
v.push(1);
v.push(2);
v.push(3);删除元素
pop删除最后一个元素
let mut v = Vec::new();
v.push(1);
v.push(2);
v.push(3);
v.pop();remove删除指定角标元素
let mut v = Vec::new();
v.push(1);
v.push(2);
v.push(3);
v.remove(0);遍历Vector
let v = vec![100, 32, 57];
for i in &v {
    println!("{}", i);
}遍历时修改
使用解引用操作符*。
let mut v = vec![100, 32, 57];
for i in &mut v {
    *i += 50;
}保存不同类型
利用enum完成不同类型存储
enum SpreadsheetCell {
    Int(i32),
    Float(f64),
    Text(String),
}
let row = vec![
    SpreadsheetCell::Int(3),
    SpreadsheetCell::Text(String::from("blue")),
    SpreadsheetCell::Float(10.12),
];
                Views: 2,844 · Posted: 2020-07-15
            
            ————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
 
        Loading...