pub struct ShiftArray<T> {
pub start: usize,
pub lst: Vec<T>,
}Expand description
The ShiftArray type represents an array that can be shifted to the left or right without copying or moving its elements.
Properties:
start: Thestartproperty represents the index of the first element in theShiftArray. It indicates the starting point from which elements are accessed or shifted.lst: Thelstproperty is a vector that holds the elements of theShiftArray. It is of typeVec<T>, whereTis a generic type parameter that can be replaced with any type.
Fields§
§start: usize§lst: Vec<T>Implementations§
Source§impl<T> ShiftArray<T>
impl<T> ShiftArray<T>
Sourcepub fn new(lst: Vec<T>) -> Self
pub fn new(lst: Vec<T>) -> Self
The function “new” initializes a new instance of a struct with a starting index of 0 and a given list.
Arguments:
lst: Thelstparameter is aVec<T>, which is a vector of elements of typeT.
Returns:
The new function is returning an instance of the struct that it is defined in.
Examples:
use mywheel_rs::array_like::ShiftArray;
let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
assert_eq!(shift_array.start, 0);
assert_eq!(shift_array.lst, vec![1, 2, 3]);
assert_eq!(shift_array.len(), 3);
assert_eq!(shift_array[0], 1);Sourcepub fn set_start(&mut self, start: usize)
pub fn set_start(&mut self, start: usize)
The function sets the start value of a variable.
Arguments:
start: Thestartparameter is of typeusize, which represents an unsigned integer that can hold the size of any object in memory. It is used to set the value of thestartfield in the struct or object that this method belongs to.
§Examples
use mywheel_rs::array_like::ShiftArray;
let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
shift_array.set_start(1);
assert_eq!(shift_array.start, 1);
assert_eq!(shift_array.len(), 3);Sourcepub fn items(&self) -> impl Iterator<Item = (usize, &T)>
pub fn items(&self) -> impl Iterator<Item = (usize, &T)>
The items function returns an iterator that yields the index and reference to each element in
the lst vector, with the index adjusted by the start value.
§Examples
use mywheel_rs::array_like::ShiftArray;
let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
shift_array.set_start(1);
for (i, v) in shift_array.items() {
assert_eq!(i, *v as usize);
}pub fn iter(&self) -> ShiftArrayIterator<'_, T> ⓘ
pub fn len(&self) -> usize
Trait Implementations§
Source§impl<T: Clone> Clone for ShiftArray<T>
impl<T: Clone> Clone for ShiftArray<T>
Source§fn clone(&self) -> ShiftArray<T>
fn clone(&self) -> ShiftArray<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for ShiftArray<T>
impl<T: Debug> Debug for ShiftArray<T>
Source§impl<T> Index<usize> for ShiftArray<T>
impl<T> Index<usize> for ShiftArray<T>
Source§fn index(&self, key: usize) -> &Self::Output
fn index(&self, key: usize) -> &Self::Output
The index function returns a reference to an element in a list based on a given key.
Arguments:
key: Thekeyparameter is of typeusizeand represents the index of the element to be accessed in thelstfield.
Returns:
The method index returns a reference to an element of self.lst at the specified index key - self.start.
§Examples
use mywheel_rs::array_like::ShiftArray;
let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
assert_eq!(shift_array[2], 3);
shift_array.set_start(1);
assert_eq!(shift_array[2], 2);Source§impl<T> IndexMut<usize> for ShiftArray<T>
impl<T> IndexMut<usize> for ShiftArray<T>
Source§fn index_mut(&mut self, key: usize) -> &mut Self::Output
fn index_mut(&mut self, key: usize) -> &mut Self::Output
The function index_mut returns a mutable reference to an element in a list based on a given
key.
Arguments:
key: Thekeyparameter is of typeusizeand represents the index of the element to be accessed in thelstvector.
Returns:
A mutable reference to an element in the lst vector, located at the index key - self.start.
§Examples
use mywheel_rs::array_like::ShiftArray;
let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
assert_eq!(shift_array[2], 3);
shift_array.set_start(1);
assert_eq!(shift_array[2], 2);
shift_array[2] = 4;
assert_eq!(shift_array[2], 4);