[Rust] Create an array of numbers by using Range

fn main() {
    let a = 0..100;

    if a.len() >= 100 {
        println!("Wow, that's a big array!");
    } else {
        println!("Meh, I eat arrays like that for breakfast.");
        panic!("Array not big enough, more elements needed")
    }
}

 

Example2:

#[test]
fn slice_out_of_array() {
    let a = [1, 2, 3, 4, 5];

    let nice_slice = &a[1..4]; // need to use borrow syntax

    assert_eq!([2, 3, 4], nice_slice)
}

 

posted @ 2024-02-23 18:57  Zhentiw  阅读(4)  评论(0编辑  收藏  举报