Trait std::slice::ImmutableVector[src]
pub trait ImmutableVector<'a, T> {
fn slice<T>(&self, start: uint, end: uint) -> &'a [T];
fn slice_from<T>(&self, start: uint) -> &'a [T];
fn slice_to<T>(&self, end: uint) -> &'a [T];
fn iter<T>(self) -> Items<'a, T>;
fn split<T>(self, pred: |&T|: 'a -> bool) -> Splits<'a, T>;
fn splitn<T>(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T>;
fn rsplitn<T>(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T>;
fn windows<T>(self, size: uint) -> Windows<'a, T>;
fn chunks<T>(self, size: uint) -> Chunks<'a, T>;
fn get<T>(&self, index: uint) -> Option<&'a T>;
fn head<T>(&self) -> Option<&'a T>;
fn tail<T>(&self) -> &'a [T];
fn tailn<T>(&self, n: uint) -> &'a [T];
fn init<T>(&self) -> &'a [T];
fn initn<T>(&self, n: uint) -> &'a [T];
fn last<T>(&self) -> Option<&'a T>;
unsafe fn unsafe_ref<T>(self, index: uint) -> &'a T;
fn as_ptr<T>(&self) -> *T;
fn bsearch<T>(&self, f: |&T| -> Ordering) -> Option<uint>;
fn shift_ref<T>(&mut self) -> Option<&'a T>;
fn pop_ref<T>(&mut self) -> Option<&'a T>;
}Extension methods for vectors
Required Methods
fn slice<T>(&self, start: uint, end: uint) -> &'a [T]
Returns a slice of self spanning the interval [start, end).
Fails when the slice (or part of it) is outside the bounds of self,
or when start > end.
fn slice_from<T>(&self, start: uint) -> &'a [T]
Returns a slice of self from start to the end of the vec.
Fails when start points outside the bounds of self.
fn slice_to<T>(&self, end: uint) -> &'a [T]
Returns a slice of self from the start of the vec to end.
Fails when end points outside the bounds of self.
fn iter<T>(self) -> Items<'a, T>
Returns an iterator over the vector
fn split<T>(self, pred: |&T|: 'a -> bool) -> Splits<'a, T>
Returns an iterator over the subslices of the vector which are
separated by elements that match pred. The matched element
is not contained in the subslices.
fn splitn<T>(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T>
Returns an iterator over the subslices of the vector which are
separated by elements that match pred, limited to splitting
at most n times. The matched element is not contained in
the subslices.
fn rsplitn<T>(self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<'a, T>
Returns an iterator over the subslices of the vector which are
separated by elements that match pred limited to splitting
at most n times. This starts at the end of the vector and
works backwards. The matched element is not contained in the
subslices.
fn windows<T>(self, size: uint) -> Windows<'a, T>
Returns an iterator over all contiguous windows of length
size. The windows overlap. If the vector is shorter than
size, the iterator returns no values.
Failure
Fails if size is 0.
Example
Print the adjacent pairs of a vector (i.e. [1,2], [2,3],
[3,4]):
let v = &[1i, 2, 3, 4]; for win in v.windows(2) { println!("{}", win); }
fn chunks<T>(self, size: uint) -> Chunks<'a, T>
Returns an iterator over size elements of the vector at a
time. The chunks do not overlap. If size does not divide the
length of the vector, then the last chunk will not have length
size.
Failure
Fails if size is 0.
Example
Print the vector two elements at a time (i.e. [1,2],
[3,4], [5]):
let v = &[1i, 2, 3, 4, 5]; for win in v.chunks(2) { println!("{}", win); }
fn get<T>(&self, index: uint) -> Option<&'a T>
Returns the element of a vector at the given index, or None if the
index is out of bounds
fn head<T>(&self) -> Option<&'a T>
Returns the first element of a vector, or None if it is empty
fn tail<T>(&self) -> &'a [T]
Returns all but the first element of a vector
fn tailn<T>(&self, n: uint) -> &'a [T]
Returns all but the first `n' elements of a vector
fn init<T>(&self) -> &'a [T]
Returns all but the last element of a vector
fn initn<T>(&self, n: uint) -> &'a [T]
Returns all but the last `n' elements of a vector
fn last<T>(&self) -> Option<&'a T>
Returns the last element of a vector, or None if it is empty.
unsafe fn unsafe_ref<T>(self, index: uint) -> &'a T
Returns a pointer to the element at the given index, without doing bounds checking.
fn as_ptr<T>(&self) -> *T
Returns an unsafe pointer to the vector's buffer
The caller must ensure that the vector outlives the pointer this function returns, or else it will end up pointing to garbage.
Modifying the vector may cause its buffer to be reallocated, which would also make any pointers to it invalid.
fn bsearch<T>(&self, f: |&T| -> Ordering) -> Option<uint>
Binary search a sorted vector with a comparator function.
The comparator function should implement an order consistent
with the sort order of the underlying vector, returning an
order code that indicates whether its argument is Less,
Equal or Greater the desired target.
Returns the index where the comparator returned Equal, or None if
not found.
fn shift_ref<T>(&mut self) -> Option<&'a T>
Returns an immutable reference to the first element in this slice and adjusts the slice in place so that it no longer contains that element. O(1).
Equivalent to:
fn main() { if self.len() == 0 { return None } let head = &self[0]; *self = self.slice_from(1); Some(head) }
if self.len() == 0 { return None }
let head = &self[0];
*self = self.slice_from(1);
Some(head)
Returns None if vector is empty
fn pop_ref<T>(&mut self) -> Option<&'a T>
Returns an immutable reference to the last element in this slice and adjusts the slice in place so that it no longer contains that element. O(1).
Equivalent to:
fn main() { if self.len() == 0 { return None; } let tail = &self[self.len() - 1]; *self = self.slice_to(self.len() - 1); Some(tail) }
if self.len() == 0 { return None; }
let tail = &self[self.len() - 1];
*self = self.slice_to(self.len() - 1);
Some(tail)
Returns None if slice is empty.
Implementors
impl<'a, T> ImmutableVector<'a, T> for &'a [T]