### Example: Get and Set Mutable References Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Demonstrates getting mutable references and modifying elements using `get_mut` and `set` methods, including handling out-of-bounds access. ```Rust # use array2d::{Array2D, Error}; let mut array = Array2D::filled_with(42, 2, 3); assert_eq!(array.get_mut(0, 0), Some(&mut 42)); assert_eq!(array.get_mut(10, 10), None); array.get_mut(0, 0).map(|x| *x = 100); assert_eq!(array.get(0, 0), Some(&100)); array.get_mut(10, 10).map(|x| *x = 200); assert_eq!(array.get(10, 10), None); ``` -------------------------------- ### Array2D Examples Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Demonstrates basic usage of the Array2D struct, including initialization and element access. ```APIDOC ## Array2D Usage Examples ### Description This section provides examples of how to use the `Array2D` struct, including creating a filled array and modifying elements. ### Code Example ```rust let mut array = Array2D::filled_with(42, 2, 3); array[(0, 0)] = 100; assert_eq!(array[(0, 0)], 100); ``` ``` -------------------------------- ### Array2D Indexing Panic Example Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Demonstrates a panic scenario when attempting to access an element with indices that are out of bounds for the Array2D. ```rust let array = Array2D::filled_with(42, 2, 3); let element = array[(10, 10)]; ``` -------------------------------- ### Example: Set Element by Row and Column Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Illustrates setting an element at specific row and column indices, showing both successful modification and error handling for out-of-bounds indices. ```Rust # use array2d::{Array2D, Error}; let mut array = Array2D::filled_with(42, 2, 3); let result = array.set(0, 0, 100); assert_eq!(result, Ok(())); assert_eq!(array.get(0, 0), Some(&100)); let result = array.set(10, 20, 200); assert_eq!(result, Err(Error::IndicesOutOfBounds(10, 20))); ``` -------------------------------- ### Example: Get Mutable Reference by Column-Major Index Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Illustrates obtaining a mutable reference to an element via its column-major index and modifying it. Shows handling of out-of-bounds indices. ```Rust # use array2d::{Array2D, Error}; # fn main() -> Result<(), Error> { let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let mut array = Array2D::from_rows(&rows)?; assert_eq!(array.get_mut_column_major(1), Some(&mut 4)); assert_eq!(array.get_mut_column_major(10), None); array.get_mut_column_major(4).map(|x| *x = 100); assert_eq!(array.get(0, 2), Some(&100)); array.get_mut_column_major(10).map(|x| *x = 200); assert_eq!(array.get(10, 10), None); # Ok(()) # } ``` -------------------------------- ### Example: Get Mutable Reference by Row-Major Index Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Demonstrates retrieving a mutable reference to an element using its row-major index and modifying it. Includes checks for out-of-bounds access. ```Rust # use array2d::{Array2D, Error}; # fn main() -> Result<(), Error> { let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let mut array = Array2D::from_rows(&rows)?; assert_eq!(array.get_mut_row_major(1), Some(&mut 2)); assert_eq!(array.get_mut_row_major(10), None); array.get_mut_row_major(3).map(|x| *x = 100); assert_eq!(array.get(1, 0), Some(&100)); array.get_mut_row_major(10).map(|x| *x = 200); assert_eq!(array.get(10, 10), None); # Ok(()) # } ``` -------------------------------- ### Array2D Out of Bounds Panic Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Example of an operation that triggers a panic due to invalid index access. ```rust let mut array = Array2D::filled_with(42, 2, 3); array[(10, 10)] = 7; ``` -------------------------------- ### Get Element by Column-Major Index Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Retrieves a reference to an element using its index in column-major order. ```rust # use array2d::{Array2D, Error}; # fn main() -> Result<(), Error> { let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; assert_eq!(array.get_column_major(2), Some(&2)); assert_eq!(array.get_column_major(4), Some(&3)); assert_eq!(array.get_column_major(10), None); # Ok(()) # } ``` -------------------------------- ### Create and manipulate Array2D Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Demonstrates creating an Array2D from various sources, indexing into it, and converting it back to vectors. ```rust use array2d::{Array2D, Error}; pub fn main() -> Result<(), Error> { // Create an array filled with the same element. let prefilled = Array2D::filled_with(42, 2, 3); assert_eq!(prefilled.num_rows(), 2); assert_eq!(prefilled.num_columns(), 3); assert_eq!(prefilled[(0, 0)], 42); // Create an array from the given rows. You can also use columns // with the `columns` function let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let from_rows = Array2D::from_rows(&rows)?; assert_eq!(from_rows.num_rows(), 2); assert_eq!(from_rows.num_columns(), 3); assert_eq!(from_rows[(1, 1)], 5); // Create an array from a flat Vec of elements in row major or // column major order. let column_major = vec![1, 4, 2, 5, 3, 6]; let from_column_major = Array2D::from_column_major(&column_major, 2, 3)?; assert_eq!(from_column_major.num_rows(), 2); assert_eq!(from_column_major.num_columns(), 3); assert_eq!(from_column_major[(1, 1)], 5); // Implements `Eq` if the element type does. assert_eq!(from_rows, from_column_major); // Index into an array using a tuple of usize to access or alter // the array. let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let mut array = Array2D::from_rows(&rows)?; array[(1, 1)] = 100; // Convert the array back into a nested Vec using `as_rows` or // `as_columns`. let array_rows = array.as_rows(); assert_eq!(array_rows, vec![vec![1, 2, 3], vec![4, 100, 6]]); // Convert the array back into a flat Vec using `as_row_major` or // `as_column_major`. let array_column_major = array.as_column_major(); ``` -------------------------------- ### Get Element by Row-Major Index Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Retrieves a reference to an element using its index in row-major order. ```rust # use array2d::{Array2D, Error}; # fn main() -> Result<(), Error> { let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; assert_eq!(array.get_row_major(2), Some(&3)); assert_eq!(array.get_row_major(4), Some(&5)); assert_eq!(array.get_row_major(10), None); # Ok(()) # } ``` -------------------------------- ### Perform common Array2D operations Source: https://docs.rs/array2d/0.3.2/array2d/index.html Demonstrates creating arrays from various sources, indexing, modifying elements, and iterating over rows and columns. ```rust use array2d::{Array2D, Error}; pub fn main() -> Result<(), Error> { // Create an array filled with the same element. let prefilled = Array2D::filled_with(42, 2, 3); assert_eq!(prefilled.num_rows(), 2); assert_eq!(prefilled.num_columns(), 3); assert_eq!(prefilled[(0, 0)], 42); // Create an array from the given rows. You can also use columns // with the `columns` function let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let from_rows = Array2D::from_rows(&rows)?; assert_eq!(from_rows.num_rows(), 2); assert_eq!(from_rows.num_columns(), 3); assert_eq!(from_rows[(1, 1)], 5); // Create an array from a flat Vec of elements in row major or // column major order. let column_major = vec![1, 4, 2, 5, 3, 6]; let from_column_major = Array2D::from_column_major(&column_major, 2, 3)?; assert_eq!(from_column_major.num_rows(), 2); assert_eq!(from_column_major.num_columns(), 3); assert_eq!(from_column_major[(1, 1)], 5); // Implements `Eq` if the element type does. assert_eq!(from_rows, from_column_major); // Index into an array using a tuple of usize to access or alter // the array. let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let mut array = Array2D::from_rows(&rows)?; array[(1, 1)] = 100; // Convert the array back into a nested Vec using `as_rows` or // `as_columns`. let array_rows = array.as_rows(); assert_eq!(array_rows, vec![vec![1, 2, 3], vec![4, 100, 6]]); // Convert the array back into a flat Vec using `as_row_major` or // `as_column_major`. let array_column_major = array.as_column_major(); assert_eq!(array_column_major, vec![1, 4, 2, 100, 3, 6]); // Iterate over a single row or column println!("First column:"); for element in array.column_iter(0)? { println!("{}", element); } // Iterate over all rows or columns. println!("All elements:"); for row_iter in array.rows_iter() { for element in row_iter { print!("{} ", element); } println!(); } Ok(()) } ``` -------------------------------- ### Array Creation Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Methods for creating new Array2D instances. ```APIDOC ## POST /api/array2d/from_iter_column_major ### Description Creates a new `Array2D` with the specified number of rows and columns and fills each element with the elements produced from the provided iterator. If the iterator produces more than enough elements, the remaining are unused. Returns an error if the iterator does not produce enough elements. The elements are inserted into the array in column major order. ### Method POST ### Endpoint /api/array2d/from_iter_column_major ### Parameters #### Query Parameters - **num_rows** (usize) - Required - The number of rows for the new Array2D. - **num_columns** (usize) - Required - The number of columns for the new Array2D. #### Request Body - **iterator** (Iterator) - Required - An iterator that produces the elements for the array. T must implement Clone. ### Request Example ```json { "iterator": "[1, 2, 3, 4, 5, 6]", "num_rows": 2, "num_columns": 3 } ``` ### Response #### Success Response (200) - **Array2D** (object) - The newly created Array2D instance. #### Response Example ```json { "array": [[1, 3, 5], [2, 4, 6]] } ``` ``` -------------------------------- ### Array2D Creation Methods Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Methods for creating new Array2D instances from various data structures and iterators. ```APIDOC ## Array2D Struct A fixed sized two-dimensional array. ## Implementations ### impl Array2D #### pub fn from_rows(elements: &[Vec]) -> Result Creates a new `Array2D` from a slice of rows, each of which is a `Vec` of elements. Returns an error if the rows are not all the same size. ##### Examples ```rust let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), rows); ``` #### pub fn from_columns(elements: &[Vec]) -> Result Creates a new `Array2D` from a slice of columns, each of which contains a `Vec` of elements. Returns an error if the columns are not all the same size. ##### Examples ```rust let columns = vec![vec![1, 4], vec![2, 5], vec![3, 6]]; let array = Array2D::from_columns(&columns)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); ``` #### pub fn from_row_major( elements: &[T], num_rows: usize, num_columns: usize, ) -> Result Creates a new `Array2D` from the given flat slice in row major order. Returns an error if the number of elements in `elements` is not the product of `num_rows` and `num_columns`, i.e. the dimensions do not match. ##### Examples ```rust let row_major = vec![1, 2, 3, 4, 5, 6]; let array = Array2D::from_row_major(&row_major, 2, 3)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); ``` #### pub fn from_column_major( elements: &[T], num_rows: usize, num_columns: usize, ) -> Result Creates a new `Array2D` from the given flat slice in column major order. Return an error if the number of elements in `elements` is not the product of `num_rows` and `num_columns`, i.e. the dimensions do not match. ##### Examples ```rust let column_major = vec![1, 4, 2, 5, 3, 6]; let array = Array2D::from_column_major(&column_major, 2, 3)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); ``` #### pub fn filled_with(element: T, num_rows: usize, num_columns: usize) -> Self Creates a new `Array2D` with the specified number of rows and columns that contains `element` in every location. ##### Examples ```rust let array = Array2D::filled_with(42, 2, 3); assert_eq!(array.as_rows(), vec![vec![42, 42, 42], vec![42, 42, 42]]); ``` #### pub fn fill_with(element: T, num_rows: usize, num_columns: usize) -> Self Deprecated since 0.2.0: Renamed to filled_with Renamed to filled_with. #### pub fn filled_by_row_major( generator: F, num_rows: usize, num_columns: usize, ) -> Self Creates a new `Array2D` with the specified number of rows and columns and fills each element with the result of calling the given function. The function is called once for every location going in row major order. ##### Examples ```rust let mut counter = 1; let increment = || { let tmp = counter; counter += 1; tmp }; let array = Array2D::filled_by_row_major(increment, 2, 3); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); ``` #### pub fn filled_by_column_major( generator: F, num_rows: usize, num_columns: usize, ) -> Self Creates a new `Array2D` with the specified number of rows and columns and fills each element with the result of calling the given function. The function is called once for every location going in column major order. ##### Examples ```rust let mut counter = 1; let increment = || { let tmp = counter; counter += 1; tmp }; let array = Array2D::filled_by_column_major(increment, 2, 3); assert_eq!(array.as_columns(), vec![vec![1, 2], vec![3, 4], vec![5, 6]]); ``` #### pub fn from_iter_row_major( iterator: I, num_rows: usize, num_columns: usize, ) -> Result Creates a new `Array2D` with the specified number of rows and columns and fills each element with the elements produced from the provided iterator. If the iterator produces more than enough elements, the remaining are unused. Returns an error if the iterator does not produce enough elements. The elements are inserted into the array in row major order. ##### Examples ```rust let iterator = (1..); let array = Array2D::from_iter_row_major(iterator, 2, 3)?; assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); ``` ``` -------------------------------- ### Get Element by Coordinates Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Retrieves a reference to an element at specific row and column indices. Returns None if the indices are out of bounds. ```rust # use array2d::{Array2D, Error}; let array = Array2D::filled_with(42, 2, 3); assert_eq!(array.get(0, 0), Some(&42)); assert_eq!(array.get(10, 10), None); ``` -------------------------------- ### Create Array2D from column major slice Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Initializes an Array2D from a flat slice using column-major ordering. Requires dimensions to match the slice length. ```rust let column_major = vec![1, 4, 2, 5, 3, 6]; let array = Array2D::from_column_major(&column_major, 2, 3)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); ``` -------------------------------- ### Iterate over specific row or column Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Methods to get an iterator for a specific row or column index. Returns an error if the index is out of bounds. ```rust let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; let mut row_iter = array.row_iter(1)?; assert_eq!(row_iter.next(), Some(&4)); assert_eq!(row_iter.next(), Some(&5)); assert_eq!(row_iter.next(), Some(&6)); assert_eq!(row_iter.next(), None); ``` ```rust let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; let mut column_iter = array.column_iter(1)?; assert_eq!(column_iter.next(), Some(&2)); assert_eq!(column_iter.next(), Some(&5)); assert_eq!(column_iter.next(), None); ``` -------------------------------- ### StructuralPartialEq Implementation Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Details the implementation of `StructuralPartialEq` for `Array2D`. ```APIDOC ## StructuralPartialEq for Array2D ### Description This implementation of `StructuralPartialEq` for `Array2D` allows for structural equality comparisons, typically used in contexts where the shape and content of data structures are important for equality. ``` -------------------------------- ### Get Mutable Reference by Row and Column Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Retrieves a mutable reference to an element at specified row and column. Returns None if indices are out of bounds. ```Rust pub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut T> { self.get_index(row, column) .map(move |index| &mut self.array[index]) } ``` -------------------------------- ### Blanket Implementations Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Details the blanket implementations provided for `Array2D`, such as `Any`, `Borrow`, `BorrowMut`, `CloneToUninit`, `From`, `Into`, `ToOwned`, `TryFrom`, and `TryInto`. ```APIDOC ## Blanket Implementations for Array2D ### Description This section covers blanket implementations, which are implementations of traits for a generic type `T` based on other traits that `T` might implement. These provide common functionalities without needing explicit implementation for `Array2D` itself. ### Implemented Traits - **Any**: Allows runtime type identification. - **Borrow**: Enables immutable borrowing. - **BorrowMut**: Enables mutable borrowing. - **CloneToUninit**: (Nightly-only) Allows cloning to uninitialized memory. - **From**: Provides conversion from `T` to `T`. - **Into**: Provides conversion into other types `U` that implement `From`. - **ToOwned**: Allows creating an owned version of the data. - **TryFrom**: Provides fallible conversion from `U` to `T`. - **TryInto**: Provides fallible conversion from `T` to `U`. ``` -------------------------------- ### Get Mutable Reference by Column-Major Index Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Retrieves a mutable reference to an element using its index in column-major order. Returns None if the index is out of bounds. ```Rust pub fn get_mut_column_major(&mut self, index: usize) -> Option<&mut T> { let column = index / self.num_rows; let row = index % self.num_rows; self.get_mut(row, column) } ``` -------------------------------- ### Initialize and Modify Array2D Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Demonstrates creating a 2D array filled with a default value and updating an element at a specific index. ```rust let mut array = Array2D::filled_with(42, 2, 3); array[(0, 0)] = 100; assert_eq!(array[(0, 0)], 100); ``` -------------------------------- ### Get Mutable Reference by Row-Major Index Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Retrieves a mutable reference to an element using its index in row-major order. Returns None if the index is out of bounds. ```Rust pub fn get_mut_row_major(&mut self, index: usize) -> Option<&mut T> { self.array.get_mut(index) } ``` -------------------------------- ### Create Array2D from column-major slice Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Initializes an Array2D from a flat slice using column-major ordering. Fails if the slice length does not match the product of rows and columns. ```rust # use array2d::{Array2D, Error}; # fn main() -> Result<(), Error> { let column_major = vec![1, 4, 2, 5, 3, 6]; let array = Array2D::from_column_major(&column_major, 2, 3)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); # Ok(()) # } ``` -------------------------------- ### Iterate Indices in Column-Major Order Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Use `indices_column_major` to get an iterator over the (row, column) indices of the Array2D in column-major order. The iterator can be collected into a Vec. ```rust let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; let indices_column_major = array.indices_column_major().collect::>(); assert_eq!(indices_column_major, vec![(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2)]); ``` -------------------------------- ### Create Array2D from columns Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Initializes an Array2D from a slice of vectors representing columns. Returns an error if column sizes are inconsistent. ```rust let columns = vec![vec![1, 4], vec![2, 5], vec![3, 6]]; let array = Array2D::from_columns(&columns)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); ``` -------------------------------- ### Iterate Indices in Row-Major Order Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Use `indices_row_major` to get an iterator over the (row, column) indices of the Array2D in row-major order. The iterator can be collected into a Vec. ```rust let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; let indices_row_major = array.indices_row_major().collect::>(); assert_eq!(indices_row_major, vec![(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]); ``` -------------------------------- ### Create Array2D from row major slice Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Initializes an Array2D from a flat slice using row-major ordering. Requires dimensions to match the slice length. ```rust let row_major = vec![1, 2, 3, 4, 5, 6]; let array = Array2D::from_row_major(&row_major, 2, 3)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); ``` -------------------------------- ### PartialEq Implementation Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Details the implementation of the `PartialEq` trait for `Array2D`, allowing for equality comparisons. ```APIDOC ## PartialEq for Array2D ### Description This implementation allows `Array2D` instances to be compared for equality using the `==` operator. It checks if both arrays have the same dimensions and if all corresponding elements are equal. ### Method `fn eq(&self, other: &Array2D) -> bool` ### Parameters - **other** (`&Array2D`) - The other `Array2D` instance to compare against. ### Returns `bool` - `true` if the arrays are equal, `false` otherwise. ``` -------------------------------- ### Iterate over Array2D elements Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Demonstrates how to iterate over specific columns or all rows within an Array2D instance. ```rust // Iterate over a single row or column println!("First column:"); for element in array.column_iter(0)? { println!("{}", element); } // Iterate over all rows or columns. println!("All elements:"); for row_iter in array.rows_iter() { for element in row_iter { print!("{} ", element); } println!(); } Ok(()) } ``` -------------------------------- ### Initialize Array2D from rows Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Creates a new Array2D instance from a vector of rows, returning an error if dimensions are inconsistent. ```rust let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; assert_eq!(array[(1, 2)], 6); ``` -------------------------------- ### Define Array2D and Error structures Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Core data structure definition and error handling implementation for the crate. ```rust pub struct Array2D { array: Vec, num_rows: usize, num_columns: usize, } #[derive(Debug, Eq, PartialEq)] pub enum Error { IndicesOutOfBounds(usize, usize), IndexOutOfBounds(usize), DimensionMismatch, NotEnoughElements, } impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Error::IndicesOutOfBounds(row, column) => write!(f, "indices ({row}, {column}) out of bounds"), Error::IndexOutOfBounds(index) => write!(f, "index {index} out of bounds"), Error::DimensionMismatch => write!(f, "dimension mismatch"), Error::NotEnoughElements => write!(f, "not enough elements"), } } } impl std::error::Error for Error {} ``` -------------------------------- ### Create Array2D filled with a constant value Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Initializes an Array2D with a specified number of rows and columns, where every element is set to the provided value. ```rust # use array2d::{Array2D, Error}; let array = Array2D::filled_with(42, 2, 3); assert_eq!(array.as_rows(), vec![vec![42, 42, 42], vec![42, 42, 42]]); ``` -------------------------------- ### Create Array2D from row-major slice Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Initializes an Array2D from a flat slice using row-major ordering. Fails if the slice length does not match the product of rows and columns. ```rust # use array2d::{Array2D, Error}; # fn main() -> Result<(), Error> { let row_major = vec![1, 2, 3, 4, 5, 6]; let array = Array2D::from_row_major(&row_major, 2, 3)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]); # Ok(()) # } ``` -------------------------------- ### Create Array2D filled with a value Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Initializes an Array2D of specified dimensions where every element is the provided value. ```rust let array = Array2D::filled_with(42, 2, 3); assert_eq!(array.as_rows(), vec![vec![42, 42, 42], vec![42, 42, 42]]); ``` -------------------------------- ### Create Array2D from rows Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Initializes an Array2D from a slice of vectors representing rows. Returns an error if row sizes are inconsistent. ```rust let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), rows); ``` -------------------------------- ### Implement TryInto for T Source: https://docs.rs/array2d/0.3.2/array2d/enum.Error.html Enables fallible conversion from type `T` to type `U`. This is the reciprocal of `TryFrom` and allows using the `.try_into()` method for conversions that might fail. ```rust type Error = >::Error ``` ```rust fn try_into(self) -> Result>::Error> ``` -------------------------------- ### Array2D::filled_with Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Creates a new Array2D with a specified number of rows and columns, filling every location with a given element. ```APIDOC ## Array2D::filled_with ### Description Creates a new [`Array2D`] with the specified number of rows and columns that contains `element` in every location. ### Method `pub fn filled_with(element: T, num_rows: usize, num_columns: usize) -> Self` ### Parameters - **element** (T) - The element to fill the array with. - **num_rows** (usize) - The number of rows for the new Array2D. - **num_columns** (usize) - The number of columns for the new Array2D. ### Request Example ```rust let array = Array2D::filled_with(42, 2, 3); ``` ### Response - **Self** (Array2D) - A new Array2D instance filled with the specified element. ### Response Example ```rust // Example of the resulting Array2D structure // vec![vec![42, 42, 42], vec![42, 42, 42]] ``` ``` -------------------------------- ### Iterate All Columns Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Provides an iterator over all columns, where each item is an iterator over the elements in that column. ```APIDOC ## GET /array2d/columns_iter ### Description Returns an `Iterator` over all columns. Each `Item` is itself another `Iterator` over references to the elements in that column. ### Method GET ### Endpoint /array2d/columns_iter ### Response #### Success Response (200) - **columns** (array of arrays of T) - An array where each inner array represents a column and contains references to its elements. #### Response Example ```json { "columns": [ [1, 4], [2, 5], [3, 6] ] } ``` ``` -------------------------------- ### Implement Into for T Source: https://docs.rs/array2d/0.3.2/array2d/enum.Error.html Enables conversion from type `T` to type `U` if `U` implements `From`. This provides a convenient way to perform conversions using the `.into()` method. ```rust fn into(self) -> U ``` -------------------------------- ### Create Array2D from iterator in column major order Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Initializes a new Array2D from an iterator, filling elements in column-major order. ```rust let iterator = (1..); let array = Array2D::from_iter_column_major(iterator, 2, 3)?; assert_eq!(array.as_rows(), vec![vec![1, 3, 5], vec![2, 4, 6]]); ``` -------------------------------- ### Collecting Array2D into Columns Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Collects the Array2D into a Vec of columns, where each column is a Vec of elements. Requires T to be Clone. ```APIDOC ## GET /websites/rs_array2d_0_3_2/as_columns ### Description Collects the `Array2D` into a `Vec` of columns, each of which contains a `Vec` of elements. ### Method GET ### Endpoint /websites/rs_array2d_0_3_2/as_columns ### Parameters None ### Request Body None ### Response #### Success Response (200) - **columns** (Vec>) - A vector of vectors representing the columns of the Array2D. #### Response Example ```json { "columns": [[1, 4], [2, 5], [3, 6]] } ``` ``` -------------------------------- ### Iterate over all columns in Array2D Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Provides an iterator over all columns, where each item is an iterator over references to elements in that column. ```rust # use array2d::{Array2D, Error}; # fn main() -> Result<(), Error> { let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows)?; for column_iter in array.columns_iter() { for element in column_iter { print!("{} ", element); } println!(); } let mut columns_iter = array.columns_iter(); let mut first_column_iter = columns_iter.next().unwrap(); assert_eq!(first_column_iter.next(), Some(&1)); assert_eq!(first_column_iter.next(), Some(&4)); assert_eq!(first_column_iter.next(), None); let mut second_column_iter = columns_iter.next().unwrap(); assert_eq!(second_column_iter.next(), Some(&2)); assert_eq!(second_column_iter.next(), Some(&5)); assert_eq!(second_column_iter.next(), None); let mut third_column_iter = columns_iter.next().unwrap(); assert_eq!(third_column_iter.next(), Some(&3)); assert_eq!(third_column_iter.next(), Some(&6)); assert_eq!(third_column_iter.next(), None); assert!(columns_iter.next().is_none()); # Ok(()) # } ``` -------------------------------- ### Auto Trait Implementations Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Lists the auto-derived trait implementations for `Array2D`, including `Freeze`, `RefUnwindSafe`, `Send`, `Sync`, `Unpin`, and `UnwindSafe`. ```APIDOC ## Auto Trait Implementations for Array2D ### Description `Array2D` automatically derives several important traits, provided that the type parameter `T` also satisfies the necessary bounds. These traits enable safe and efficient memory management and concurrency. ### Implemented Traits - **Freeze**: Indicates that the type does not contain interior mutability and can be frozen. - **RefUnwindSafe**: Indicates that references to the type are safe to unwind across. - **Send**: Indicates that the type can be safely transferred between threads. - **Sync**: Indicates that the type can be safely shared between threads. - **Unpin**: Indicates that the type does not require special handling for pinning. - **UnwindSafe**: Indicates that the type is safe to unwind across. ``` -------------------------------- ### Array Information Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Methods for retrieving information about the Array2D dimensions and size. ```APIDOC ## GET /api/array2d/info ### Description Retrieves information about the Array2D, including the number of rows, columns, total elements, row length, and column length. ### Method GET ### Endpoint /api/array2d/info ### Parameters None ### Response #### Success Response (200) - **num_rows** (usize) - The number of rows in the array. - **num_columns** (usize) - The number of columns in the array. - **num_elements** (usize) - The total number of elements (rows * columns). - **row_len** (usize) - The number of elements in each row (equal to num_columns). - **column_len** (usize) - The number of elements in each column (equal to num_rows). #### Response Example ```json { "num_rows": 2, "num_columns": 3, "num_elements": 6, "row_len": 3, "column_len": 2 } ``` ``` -------------------------------- ### Collect Array2D into Vec of Columns Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Use `as_columns` to collect the Array2D into a Vec of Vecs, where each inner Vec represents a column. Requires T to implement Clone. ```rust let columns = vec![vec![1, 4], vec![2, 5], vec![3, 6]]; let array = Array2D::from_columns(&columns)?; assert_eq!(array.as_columns(), columns); ``` -------------------------------- ### Create Array2D using a column-major generator Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Populates an Array2D by calling a generator function for each position in column-major order. ```rust # use array2d::{Array2D, Error}; let mut counter = 1; let increment = || { let tmp = counter; counter += 1; tmp }; let array = Array2D::filled_by_column_major(increment, 2, 3); assert_eq!(array.as_columns(), vec![vec![1, 2], vec![3, 4], vec![5, 6]]); ``` -------------------------------- ### Create Array2D filled by column major generator Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Initializes an Array2D by calling a generator function for each position in column-major order. ```rust let mut counter = 1; let increment = || { let tmp = counter; counter += 1; tmp }; let array = Array2D::filled_by_column_major(increment, 2, 3); assert_eq!(array.as_columns(), vec![vec![1, 2], vec![3, 4], vec![5, 6]]); ``` -------------------------------- ### as_columns Source: https://docs.rs/array2d/0.3.2/src/array2d/lib.rs.html Collects the Array2D into a Vec of columns, where each column is a Vec of elements. ```APIDOC ## as_columns ### Description Collects the [`Array2D`] into a [`Vec`] of columns, each of which contains a [`Vec`] of elements. ### Method `pub fn as_columns(&self) -> Vec>` ### Constraints `T: Clone` ### Request Example ```rust let columns = vec![vec![1, 4], vec![2, 5], vec![3, 6]]; let array = Array2D::from_columns(&columns)?; assert_eq!(array.as_columns(), columns); ``` ### Response #### Success Response (200) - `Vec>`: A vector of vectors representing the columns of the Array2D. ### Response Example ```json { "example": "[[1, 4], [2, 5], [3, 6]]" } ``` ``` -------------------------------- ### Array2D Panics Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Explains the conditions under which the Array2D struct will panic, specifically related to out-of-bounds indexing. ```APIDOC ## Array2D Panics ### Description The `Array2D` struct panics if the provided indices are out of the bounds of the array. ### Panic Condition Accessing or modifying an element using indices that fall outside the dimensions of the `Array2D`. ### Example ```rust let mut array = Array2D::filled_with(42, 2, 3); // This will panic because the indices (10, 10) are out of bounds for a 2x3 array. // array[(10, 10)] = 7; ``` ``` -------------------------------- ### Error Trait Implementations Source: https://docs.rs/array2d/0.3.2/array2d/enum.Error.html Details on the trait implementations for the Error enum, including Debug, Display, and Error. ```APIDOC ### Trait Implementations #### impl Debug for Error * `fn fmt(&self, f: &mut Formatter<'_>) -> Result`: Formats the value using the given formatter. #### impl Display for Error * `fn fmt(&self, f: &mut Formatter<'_>) -> Result`: Formats the value using the given formatter. #### impl Error for Error * `fn source(&self) -> Option<&(dyn Error + 'static)>`: Returns the lower-level source of this error, if any. * `fn description(&self) -> &str`: Deprecated since 1.42.0: use the Display impl or to_string(). * `fn cause(&self) -> Option<&dyn Error>`: Deprecated since 1.33.0: replaced by Error::source, which can support downcasting. * `fn provide<'a>(&'a self, request: &mut Request<'a>)`: This is a nightly-only experimental API. (`error_generic_member_access`). Provides type-based access to context intended for error reports. #### impl PartialEq for Error * `fn eq(&self, other: &Error) -> bool`: Tests for `self` and `other` values to be equal, and is used by `==`. * `fn ne(&self, other: &Rhs) -> bool`: Tests for `!=`. #### impl Eq for Error #### impl StructuralPartialEq for Error ``` -------------------------------- ### Iterate over all elements Source: https://docs.rs/array2d/0.3.2/array2d/struct.Array2D.html Methods to retrieve an iterator over all elements in the array in either row-major or column-major order. ```rust let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let elements = vec![1, 2, 3, 4, 5, 6]; let array = Array2D::from_rows(&rows)?; let row_major = array.elements_row_major_iter(); assert_eq!(row_major.cloned().collect::>(), elements); ``` ```rust let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let elements = vec![1, 4, 2, 5, 3, 6]; let array = Array2D::from_rows(&rows)?; let column_major = array.elements_column_major_iter(); assert_eq!(column_major.cloned().collect::>(), elements); ``` -------------------------------- ### Implement PartialEq for Error Enum Source: https://docs.rs/array2d/0.3.2/array2d/enum.Error.html Enables comparison of two Error enum instances for equality. This is automatically derived for most enums but can be explicitly implemented if custom logic is needed. ```rust fn eq(&self, other: &Error) -> bool ``` ```rust fn ne(&self, other: &Rhs) -> bool ``` -------------------------------- ### Implement TryFrom for T Source: https://docs.rs/array2d/0.3.2/array2d/enum.Error.html Enables fallible conversion from type `U` to type `T`. This is used when a conversion might fail and needs to return a `Result`. ```rust type Error = Infallible ``` ```rust fn try_from(value: U) -> Result>::Error> ``` -------------------------------- ### Implement Debug for Error Enum Source: https://docs.rs/array2d/0.3.2/array2d/enum.Error.html Provides a debug representation for the Error enum, allowing it to be printed using the `{:?}` format specifier. This is useful for debugging purposes. ```rust fn fmt(&self, f: &mut Formatter<'_>) -> Result ```