`

rust 线性序列-数组

    博客分类:
  • rust
 
阅读更多
fn main() {
    //vec!是一个宏,用来创建向量字面量,宏语句可以使用圆括号,也可以使用中括号和花括号,一般使用中括号表示数组
    let mut v1 = vec![];//vec!{},vec!()
    v1.push(1);
    assert_eq!(v1[0],1);
    let mut v2 = vec![0;10];
    assert_eq!(v2[3],0);
}


fn testFnVec(v :&[i32]) {
    for vv in v {
        print!("{} ",vv)
    }
}

fn main() {
    let mut v = vec![1, 2, 3, 4, 5];
    v.push(6);
    v.remove(0);
    for  vv in &mut v {
        *vv += 10;
    }

    testFnVec(&v);
    println!();

    let third: &i32 = &v[2];
    println!("The third element is {}", third);

    match v.get(0) {
        Some(vv) => println!("index value is {}", vv),
        None => println!("not found")
    }

    match v.get(5) {
        Some(vv) => println!("index value is {}", vv),
        None => println!("not found")
    }

    println!("len:{}",v.len());
    println!("empty:{}",v.is_empty())
}

12 13 14 15 16 
The third element is 14
index value is 12
not found
len:5
empty:false
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics