Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Grid::new</code> should initialize <code>Grid::inner</code> 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 <code>Cell</code> type:</p> <pre><code>impl Grid { fn new -&gt; Grid { Grid { inner: [[alive, ..GRID_SIZE], ..GRID_SIZE] } } } </code></pre> <p>(You can use <code>Cell::new()</code> instead of <code>alive</code> if you'd rather use the constructor function.)</p> <p>The <code>inner</code> member can then be used in expressions as follows (note that <code>priv</code> only controls visibility to code outside the current module):</p> <pre><code>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]; </code></pre> <p>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 <a href="http://static.rust-lang.org/doc/master/tutorial.html#vectors-and-strings">Vectors and Strings</a>. In particular, unlike vectors (of type <code>~[T]</code>), 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 (<code>[T, ..LENGTH]</code>), so that they can't change size after being created. <code>T</code> 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 <code>struct</code> definitions (like <code>Grid</code>), etc.</p> <p>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 <code>Grid::inner</code> is exactly <code>GRID_SIZE * GRID_SIZE * sizeof(Cell)</code> (if we ignore alignment). Also useful is a vector of fixed-size arrays <code>~[T, ..LENGTH]</code>, when you know the number of columns in a matrix but not the number of rows.</p> <p>Both vectors and fixed-size arrays can serve as a function argument if the argument is a slice (type <code>&amp;[T]</code>).</p> <p>Some details may change between now and the release of Rust 1.0. If you're curious, searching the <a href="http://reddit.com/r/rust">Rust subreddit</a> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload