How to create a static array of strings?

前端 未结 4 1259
情书的邮戳
情书的邮戳 2021-02-03 18:45

Note This question contains syntax that predates Rust 1.0. The code is invalid, but the concepts are still relevant.

How do

4条回答
  •  猫巷女王i
    2021-02-03 19:36

    Just used this to allocate a small POC level for a game in Rust

    const LEVEL_0: &'static [&'static [i32]] = &[
        &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        &[1, 9, 0, 0, 0, 2, 0, 0, 3, 1],
        &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        &[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ];
    

    And loaded using the following function

    pub fn load_stage(&mut self, ctx: &mut Context, level: usize) {
        let levels = vec![LEVEL_0];
    
        for (i, row) in levels[level].iter().enumerate() {
            for (j, col) in row.iter().enumerate() {
                if *col == 1 {
                    self.board.add_block(
                        ctx,
                        Vector2::::new(j as f32, i as f32),
                        self.cell_size,
                    );
                }
    

提交回复
热议问题