pub struct Row<'a, T> {
pub grid: &'a Grid<T>,
pub index: usize,
}Expand description
A view onto a row of a grid
This structure is an immutable view into a row of a grid and its lifetime is bound to the lifetime of the grid. It’s a lightweight construct that allows to operate on individual rows effectively; see it as an equivalent to the slice primitive for grids.
Instead of accessing elements with coordinates, just an index is needed. Rows use the left to right direction, therefore, index zero corresponds to the element at the very left, also denoted the ‘first’ element of the row. Note that rows are indexable.
With a row, you can easily retrieve the left and right elements of the row,
with the left() and right() methods, but also retrieve the row above or
below, with the top() and bottom() methods. You can also conveniently
iterate over its elements with the iterator() method which returns an
efficient iterator.
Because this view is immutable, it’s limited in terms of what it can do; check out the mutable counter-part for more operations over the rows.
§Examples
Iterating over the elements of a row.
let grid = Grid::from_rows(vec![vec![1, 2, 3],
vec![4, 5, 6]]);
let row = grid.row(0);
for (coordinate, value) in row.iterator().enumerate_coordinate() {
println!("Element at {:?} has value {}.", coordinate, *value);
}Indexing the row.
let grid = Grid::from_rows(vec![vec![1, 2, 3],
vec![4, 5, 6]]);
println!("First element of first row is {}", grid.row(0)[0]);
println!("Last element of last row is {}", grid.row(1)[2]);Fields§
§grid: &'a Grid<T>A reference to its grid.
index: usizeThe index of the row.
Implementations§
Source§impl<'a, T: Clone> Row<'a, T>
impl<'a, T: Clone> Row<'a, T>
Sourcepub fn length(&self) -> usize
pub fn length(&self) -> usize
Returns the length of the row.
This method returns the length of the row which is the number of elements. It’s equivalent to the width of the grid.
§Examples
let grid = Grid::with_size(size!(3, 2), 42);
assert_eq!(grid.row(0).length(), 3);
assert_eq!(grid.row(1).length(), 3);
assert_eq!(grid.size().width, 3);Sourcepub fn value(&self, index: usize) -> &'a T
pub fn value(&self, index: usize) -> &'a T
Returns a reference to an element of the row.
This method returns a reference to an element of the row from its index.
Note that index zero corresponds to the element at the very left (the
first element of the row). If you’re looking to get the first or the
last elements of the row, check out the left() and right() methods.
§Arguments
index- Index of the element
§Panics
It panics if the index is out of bounds.
§Examples
let grid = Grid::from_rows(vec![vec![1, 2, 3],
vec![4, 5, 6]]);
let row = grid.row(1);
assert_eq!(row.value(0), &4);
assert_eq!(row.value(1), &5);
assert_eq!(row.value(2), &6);
row.value(3); // It panics here !Sourcepub fn values(&self) -> Vec<&T>
pub fn values(&self) -> Vec<&T>
Return the elements of the row.
This method returns the elements of the row as a vector of reference.
§Examples
let grid = Grid::from_rows(vec![vec![1, 2],
vec![3, 4]]);
assert_eq!(grid.row(0).values(), vec![&1, &2]);
assert_eq!(grid.row(1).values(), vec![&3, &4]);Sourcepub fn left(&self) -> &T
pub fn left(&self) -> &T
Returns a reference to the first element of the row.
This method returns a reference to the first element of the row. It’s
equivalent to retrieving the element with index 0.
Note that there is always a first element or the grid would have no size.
§Examples
let grid = Grid::from_rows(vec![vec![1, 2, 3],
vec![4, 5, 6]]);
// The first element of the second row is 4.
let row = grid.row(1);
assert_eq!(row.left(), &4);Sourcepub fn right(&self) -> &T
pub fn right(&self) -> &T
Returns a reference to the last element of the row.
This method returns a reference to the last element of the row. It’s
equivalent to retrieving the element with index length() -1.
Note that there is always a last element or the grid would have no size.
§Examples
let grid = Grid::from_rows(vec![vec![1, 2, 3],
vec![4, 5, 6]]);
// The last element of the second row is 6.
let row = grid.row(1);
assert_eq!(row.right(), &6);Sourcepub fn iterator(&self) -> IteratorRow<'a, T> ⓘ
pub fn iterator(&self) -> IteratorRow<'a, T> ⓘ
Returns an iterator over the row.
This method returns an iterator over the row.
§Examples
let grid = Grid::from_rows(vec![vec![ 1, 2, 3],
vec![42, 42, 42]]);
// Check if all elements of the row have value 42.
assert_eq!(grid.row(0).iterator().all(|item| *item == 42), false);
assert_eq!(grid.row(1).iterator().all(|item| *item == 42), true);Sourcepub fn top(&self) -> Option<Row<'a, T>>
pub fn top(&self) -> Option<Row<'a, T>>
Returns the row above.
This method returns the row above this row, or None if this is already
the row at the very top of the grid.
§Examples
let grid = Grid::from_rows(vec![vec![1, 2],
vec![3, 4]]);
let second_row = grid.row(1);
let first_row = second_row.top().unwrap();
assert!(first_row.top().is_none()); // There is no row above.Sourcepub fn bottom(&self) -> Option<Row<'a, T>>
pub fn bottom(&self) -> Option<Row<'a, T>>
Returns the row below.
This method returns the row below this row, or None if this is already
the row at the very bottom of the grid.
§Examples
let grid = Grid::from_rows(vec![vec![1, 2],
vec![3, 4]]);
let first_row = grid.row(0);
let second_row = first_row.bottom().unwrap();
assert!(second_row.bottom().is_none()); // There is no row below.