如何在结构中初始化这个数组数组? [英] How do I initialize this array of arrays within a struct?

查看:58
本文介绍了如何在结构中初始化这个数组数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从教程中学习 Rust.我认为康威的人生游戏会是一个很好的起点.

I'm trying to learn rust from the tutorial. I thought Conway's game of life would be a good place to start.

我无法理解如何编写这个 Grid::new() fn.

I'm having trouble understanding how to write this Grid::new() fn.

这是我目前所拥有的:

enum Cell {
    alive, dead
}
impl Cell {
    fn new() -> Cell {
        alive
    }
struct Grid {
    priv inner: [ [Cell, .. GRID_SIZE], .. GRID_SIZE],
}

impl Grid {
    fn new() {
        Grid { inner: ???? }
    }
}

...

fn main () {
    let grid = Grid::new(); // Stack allocated grid (internal stack allocad array)
}

我想要的是使用所有值为活着"的单元格初始化网格.

What I want is for the grid to be initialized with cells all of the value 'alive'.

推荐答案

Grid::new 应该用嵌套的固定大小的数组字面量初始化 Grid::inner,它的写法和类型一样,但是用你想要初始化数组的值代替 Cell 类型:

Grid::new should initialize Grid::inner with a nested fixed-size array literal, which is written just like the type, but with the value you want to initialize your array with in place of the Cell type:

impl Grid {
    fn new -> Grid {
        Grid { inner: [[alive, ..GRID_SIZE], ..GRID_SIZE] }
    }
}

(如果您更愿意使用构造函数,您可以使用 Cell::new() 而不是 alive.)

(You can use Cell::new() instead of alive if you'd rather use the constructor function.)

inner 成员然后可以在表达式中使用,如下所示(注意 priv 只控制当前模块外代码的可见性):

The inner member can then be used in expressions as follows (note that priv only controls visibility to code outside the current module):

let grid = Grid::new();
let nested_fixed_sized_array: [[Cell, ..GRID_SIZE], ..GRID_SIZE] = grid.inner;
let fixed_sized_array: [Cell, ..GRID_SIZE] = grid.inner[0];
let cell_element: Cell = grid.inner[0][0];

在 Rust 中,本例中使用的嵌套数组是固定大小数组的特殊情况.要了解其工作原理,请参阅关于向量和字符串的教程部分.特别是,与在堆上动态分配并且可以改变其长度(如果它们是可变的)的向量(类型 ~[T])不同,固定大小的数组的长度嵌入在类型 ([T, ..LENGTH]),这样它们在创建后就不能改变大小.T 本身必须是固定大小的类型或指针.然而,作为交换,固定大小的数组是可以直接在堆栈上分配、嵌入在 struct 定义(如 Grid)等中的值类型.

In Rust, nested arrays, as used in this example, are a special case of fixed-size arrays. To see how this works, see the tutorial section on Vectors and Strings. In particular, unlike vectors (of type ~[T]), which are dynamically allocated on the heap and can change their length (if they're mutable), fixed-size arrays have their length embedded in the type ([T, ..LENGTH]), so that they can't change size after being created. T itself must be either a fixed-size type or a pointer. In exchange, however, fixed-size arrays are value types that can be directly allocated on the stack, embedded in struct definitions (like Grid), etc.

由于固定大小数组本身就是固定大小类型,因此嵌套固定大小数组只是一种特殊情况,其中固定大小数组是固定大小数组的元素类型.特别是,Grid::inner 占用的内存正好是 GRID_SIZE * GRID_SIZE * sizeof(Cell)(如果我们忽略对齐的话).当您知道矩阵中的列数但不知道行数时,固定大小数组的向量 ~[T, ..LENGTH] 也很有用.

Since a fixed-size array is itself a fixed-size type, a nested fixed-size array is just a special case where a fixed-size array is the element type of a fixed-size array. In particular, the memory taken up by Grid::inner is exactly GRID_SIZE * GRID_SIZE * sizeof(Cell) (if we ignore alignment). Also useful is a vector of fixed-size arrays ~[T, ..LENGTH], when you know the number of columns in a matrix but not the number of rows.

如果参数是一个切片(类型 &[T]),那么向量和固定大小的数组都可以作为函数参数.

Both vectors and fixed-size arrays can serve as a function argument if the argument is a slice (type &[T]).

从现在到 Rust 1.0 发布,一些细节可能会发生变化.如果你很好奇,在 Rust subreddit 中搜索动态大小的类型"应该会发现提议的更改及其背后的原因,或者你可以随时在 Reddit 或 #rust IRC 频道上询问.

Some details may change between now and the release of Rust 1.0. If you're curious, searching the Rust subreddit for "dynamically sized types" should turn up the proposed changes and the reasoning behind them, or you can always ask around on Reddit or the #rust IRC channel.

这篇关于如何在结构中初始化这个数组数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆