Slice

Struct Slice 

Source
pub struct Slice<'s> { /* private fields */ }
Expand description

A ‘lazy’ str slice using Ranges.

Implementations§

Source§

impl<'s> Slice<'s>

Source

pub const fn new(source: &'s str) -> Slice<'s>

Source

pub const fn len(&self) -> usize

Returns the length of self.

This length is in bytes, not chars or graphemes. In other words, it may not be what a human considers the length of the string.

§Examples

Basic usage:

let len = Slice::new("foo").len();
assert_eq!(3, len);

assert_eq!(Slice::new("ƒoo").len(), 4); // fancy f!
Source

pub const fn is_empty(&self) -> bool

Returns true if self has a length of zero bytes.

§Examples

Basic usage:

let s = Slice::new("");
assert!(s.is_empty());

let s = Slice::new("not empty");
assert!(!s.is_empty());
Source

pub fn starts_with<'r, P, F>(&self, pat: P) -> bool
where P: Into<Pattern<'r, F>>, F: FnMut(char) -> bool,

Returns true if the given pattern matches a prefix of this string slice.

Returns false if it does not.

The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.

§Examples

Basic usage:

let bananas = Slice::new("bananas");

assert!(bananas.starts_with("bana"));
assert!(!bananas.starts_with("nana"));
Source

pub fn ends_with<'r, P, F>(&self, pat: P) -> bool
where P: Into<Pattern<'r, F>>, F: FnMut(char) -> bool,

Returns true if the given pattern matches a suffix of this string slice.

Returns false if it does not.

The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.

§Examples

Basic usage:

let bananas = Slice::new("bananas");

assert!(bananas.ends_with("anas"));
assert!(!bananas.ends_with("nana"));
Source

pub fn contains<'r, P, F>(&self, pat: P) -> bool
where P: Into<Pattern<'r, F>>, F: FnMut(char) -> bool,

Returns true if the given pattern matches a sub-slice of this string slice.

Returns false if it does not.

The pattern can be a &str, char, a slice of chars, or a function or closure that determines if a character matches.

§Examples

Basic usage:

let bananas = Slice::new("bananas");

assert!(bananas.contains("nana"));
assert!(!bananas.contains("apples"));
Source

pub fn trim(&self) -> Slice<'s>

Returns a string slice with leading and trailing whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

§Examples

Basic usage:

let s = Slice::new(" Hello\tworld\t");

assert_eq!("Hello\tworld", s.trim().slice());
Source

pub fn trim_start(&self) -> Slice<'s>

Returns a string slice with leading whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

§Text directionality

A string is a sequence of bytes. start in this context means the first position of that byte string; for a left-to-right language like English or Russian, this will be left side, and for right-to-left languages like Arabic or Hebrew, this will be the right side.

§Examples

Basic usage:

let s = Slice::new(" Hello\tworld\t");
assert_eq!("Hello\tworld\t", s.trim_start().slice());

Directionality:

let s = Slice::new("  English  ");
assert!(Some('E') == s.trim_start().slice().chars().next());

let s = Slice::new("  עברית  ");
assert!(Some('ע') == s.trim_start().slice().chars().next());
Source

pub fn trim_end(&self) -> Slice<'s>

Returns a string slice with trailing whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

§Text directionality

A string is a sequence of bytes. end in this context means the last position of that byte string; for a left-to-right language like English or Russian, this will be right side, and for right-to-left languages like Arabic or Hebrew, this will be the left side.

§Examples

Basic usage:

let s = Slice::new(" Hello\tworld\t");
assert_eq!(" Hello\tworld", s.trim_end().slice());

Directionality:

let s = Slice::new("  English  ");
assert!(Some('h') == s.trim_end().slice().chars().rev().next());

let s = Slice::new("  עברית  ");
assert!(Some('ת') == s.trim_end().slice().chars().rev().next());
Source

pub fn index<R>(&self, range: R) -> Slice<'s>
where R: Into<Ranges>,

It isn’t possible to return a owned Slice from a Index, so you have to use this function.

Source

pub fn slice(self) -> &'s str

Consume and ‘run’ the slice, returning the given range of the source str.

Trait Implementations§

Source§

impl<'s> Clone for Slice<'s>

Source§

fn clone(&self) -> Self

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<'s> Debug for Slice<'s>

Source§

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

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

impl<'s> Display for Slice<'s>

Source§

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

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

impl<'s> Hash for Slice<'s>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'s> PartialEq for Slice<'s>

Source§

fn eq(&self, other: &Slice<'s>) -> 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<'s> Eq for Slice<'s>

Source§

impl<'s> StructuralPartialEq for Slice<'s>

Auto Trait Implementations§

§

impl<'s> Freeze for Slice<'s>

§

impl<'s> RefUnwindSafe for Slice<'s>

§

impl<'s> Send for Slice<'s>

§

impl<'s> Sync for Slice<'s>

§

impl<'s> Unpin for Slice<'s>

§

impl<'s> UnwindSafe for Slice<'s>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoOption for T

Source§

fn some_if(self, predicate: bool) -> Option<Self>

Results Some(self) if the predicate returns true, or None otherwise.
Source§

fn with_some_if<F>(self, predicate: F) -> Option<Self>
where F: FnOnce(&Self) -> bool,

Results Some(self) if the predicate returns true, or None otherwise.
Source§

fn none_if(self, predicate: bool) -> Option<Self>

Results None if the predicate returns true, or Some(self) otherwise.
Source§

fn with_none_if<F>(self, predicate: F) -> Option<Self>
where F: FnOnce(&Self) -> bool,

Results None if the predicate returns true, or Some(self) otherwise.
Source§

impl<T> IntoResult for T

Source§

fn ok_if<E>(self, predicate: bool, err: E) -> Result<Self, E>

Results Ok(self) if the predicate returns true, or Err(err) otherwise.
Source§

fn with_ok_if<F, E>(self, predicate: F, err: E) -> Result<Self, E>
where F: FnOnce(&Self) -> bool,

Results Ok(self) if the predicate returns true, or Err(err) otherwise.
Source§

fn err_if<E>(self, predicate: bool, err: E) -> Result<Self, E>

Results Err(err) if the predicate returns true, or Ok(self) otherwise.
Source§

fn with_err_if<F, E>(self, predicate: F, err: E) -> Result<Self, E>
where F: FnOnce(&Self) -> bool,

Results Err(err) if the predicate returns true, or Ok(self) otherwise.
Source§

impl<T> Peep for T

Source§

fn peep<F, R>(self, run: F) -> Self
where F: FnOnce(&Self) -> R, R: Sized,

Source§

fn peep_dbg<F, R>(self, run: F) -> Self
where F: FnOnce(&Self) -> R, R: Sized,

Source§

fn peep_mut<F, R>(self, run: F) -> Self
where F: FnOnce(&mut Self) -> R, R: Sized,

Source§

fn peep_mut_dbg<F, R>(self, run: F) -> Self
where F: FnOnce(&mut Self) -> R, R: Sized,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Wrap for T

Source§

fn wrap_ref<F>(self, wrap: F) -> Self
where F: FnOnce(&Self),

Turns a self reference function call into an ‘inline’/‘builder’ call. Read more
Source§

fn wrap_mut<F>(self, wrap: F) -> Self
where F: FnOnce(&mut Self),

Turns a self mutable reference function call into an ‘inline’/‘builder’ call. Read more
Source§

fn wrap_map<F, R>(self, wrap: F) -> R
where F: FnOnce(Self) -> R,

Turns a consuming function call into an ‘inline’/‘builder’ call. Read more