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_rows
andfrom_columns
). - Providing a "flat" slice of elements along with the dimensions, which
must match the number of elements (see
from_row_major
andfrom_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_iter
andelements_column_major_iter
). - Individual rows or columns (see
row_iter
andcolumn_iter
). - All rows or all columns (see
rows_iter
andcolumns_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
Vec
of rows or columns (seeas_rows
andas_columns
). - A "flat"
Vec
of elements in either row major or column major order (seeas_row_major
andas_column_major
).
Examples
use Array2D;