Array2D
Array2D provides a statically-sized two-dimensional array. It is more
efficient and is easier to use than nested vectors, i.e. Vec<Vec<T>>.
This is beneficial when using a grid-like structure, which is common in image processing, game boards, and other situations. This cannot be used when rows or columns might have different lengths, as they are required to all be the same length.
How to use Array2D
Creating an Array2D
An Array2D can be created by either pre-filling it with a repeated value
or by providing it with the data to create the array with. This can be done
by:
- Providing the rows or the columns, which must all be the same size (see
from_rowsandfrom_columns). - Providing a "flat" slice of elements along with the dimensions, which
must match the number of elements (see
from_row_majorandfrom_column_major).
Accessing data from an Array2D
Array2D supports indexing using a tuple of (usize, usize) (which
panics on out-of-bounds accesses) or through the get, get_mut, and
set methods (which return an Option or a Result on out-of-bounds
accesses)
Array2D also supports several forms of iteration. You can iterate
through:
- All of the elements, in either row major or column major order (see
elements_row_major_iterandelements_column_major_iter). - Individual rows or columns (see
row_iterandcolumn_iter). - All rows or all columns (see
rows_iterandcolumns_iter).
Extracting all data from an Array2D
An Array2D can be converted back into a Vec through several
methods. You can extract the data as:
- A
Vecof rows or columns (seeas_rowsandas_columns). - A "flat"
Vecof elements in either row major or column major order (seeas_row_majorandas_column_major).
Examples
use Array2D;