pub struct Slice<'s> { /* private fields */ }Implementations§
Source§impl<'s> Slice<'s>
impl<'s> Slice<'s>
pub const fn new(source: &'s str) -> Slice<'s>
Sourcepub const fn is_empty(&self) -> bool
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());Sourcepub fn starts_with<'r, P, F>(&self, pat: P) -> bool
pub fn starts_with<'r, P, F>(&self, pat: P) -> 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"));Sourcepub fn ends_with<'r, P, F>(&self, pat: P) -> bool
pub fn ends_with<'r, P, F>(&self, pat: P) -> 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"));Sourcepub fn contains<'r, P, F>(&self, pat: P) -> bool
pub fn contains<'r, P, F>(&self, pat: P) -> 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"));Sourcepub fn trim(&self) -> Slice<'s>
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());Sourcepub fn trim_start(&self) -> Slice<'s>
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());Sourcepub fn trim_end(&self) -> Slice<'s>
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());Trait Implementations§
impl<'s> Eq for Slice<'s>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoOption for T
impl<T> IntoOption for T
Source§fn some_if(self, predicate: bool) -> Option<Self>
fn some_if(self, predicate: bool) -> Option<Self>
Some(self) if the predicate returns true, or None otherwise.Source§fn with_some_if<F>(self, predicate: F) -> Option<Self>
fn with_some_if<F>(self, predicate: F) -> Option<Self>
Some(self) if the predicate returns true, or None otherwise.Source§impl<T> IntoResult for T
impl<T> IntoResult for T
Source§fn ok_if<E>(self, predicate: bool, err: E) -> Result<Self, E>
fn ok_if<E>(self, predicate: bool, err: E) -> Result<Self, E>
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>
fn with_ok_if<F, E>(self, predicate: F, err: E) -> Result<Self, E>
Ok(self) if the predicate returns true, or Err(err) otherwise.