Boden Garman
Boden Garman

Reputation: 2545

Multidimensional vectors in rust, segfaulting?

In C# I could do something like this

class Map {    
    public Tile[, ,] Location = new Tile[6, 256, 256];
}

and later access any Tile element with something like Location[2, 40, 20]

I'm trying to make similar type of structure in Rust but I'm finding the syntax a bit odd. I did come up with this, but it segfaulted with large vector sizes (ran out of stack?):

use tile::Tile // Simple struct with a few Point properties

pub struct Map {
    location: [[[Tile, ..256], ..256], ..6],
}

pub impl Map {
    fn new() -> Map {
        Map {
            // assuming empty_tile is a Tile I've just created
            location: [[[empty_tile, ..256], ..256], ..6]
        }
    }
}

Am I going about this incorrectly? It ran really slowly and large sizes segfaulted. Perhaps there's a better way to store a three dimensional space of tiles (layer, width, height)?

edit: this is before I even try and make those Tiles mutable

Upvotes: 3

Views: 973

Answers (1)

ember arlynx
ember arlynx

Reputation: 3291

[[[Tile, ..256], ..256], ..6] is stack allocated, and it's going to be probably ~60MB large. Try ~[~[~[Tile, ..256], ..256], ..6], which is an owned pointer (allocated on the send heap, owned pointers can be sent across tasks)

Upvotes: 4

Related Questions