Skip to main content

Row

Struct Row 

Source
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: usize

The index of the row.

Implementations§

Source§

impl<'a, T: Clone> Row<'a, T>

Source

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);
Source

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 !
Source

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]);
Source

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);
Source

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);
Source

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);
Source

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.
Source

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.

Trait Implementations§

Source§

impl<'a, T: Clone> Clone for Row<'a, T>

Source§

fn clone(&self) -> Row<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T: Debug> Debug for Row<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: Clone> Index<usize> for Row<'a, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T: PartialEq> PartialEq for Row<'a, T>

Source§

fn eq(&self, other: &Row<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T: Eq> Eq for Row<'a, T>

Source§

impl<'a, T> StructuralPartialEq for Row<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for Row<'a, T>

§

impl<'a, T> RefUnwindSafe for Row<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Row<'a, T>
where T: Sync,

§

impl<'a, T> Sync for Row<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Row<'a, T>

§

impl<'a, T> UnsafeUnpin for Row<'a, T>

§

impl<'a, T> UnwindSafe for Row<'a, T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.