pub struct StrView<'a> { /* private fields */ }Expand description
Truly zero-copy string reference.
This is just a pointer and length (16 bytes on 64-bit systems),
making it extremely cheap to copy and pass around. Unlike &str,
it can be stored in structs without lifetime issues when the
underlying buffer is known to outlive the view.
§Safety
The underlying data must:
- Be valid UTF-8
- Remain valid for the lifetime
'a - Not be modified while the view exists
Implementations§
Source§impl<'a> StrView<'a>
impl<'a> StrView<'a>
Sourcepub const unsafe fn from_raw_parts(ptr: *const u8, len: usize) -> Self
pub const unsafe fn from_raw_parts(ptr: *const u8, len: usize) -> Self
Create a string view from raw parts.
§Safety
The caller must ensure:
ptrpoints to valid UTF-8 data- The data is at least
lenbytes - The data remains valid for lifetime
'a
Sourcepub fn slice(&self, range: Range<usize>) -> StrView<'a>
pub fn slice(&self, range: Range<usize>) -> StrView<'a>
Get a subview by byte range.
§Panics
Panics if:
- The range is out of bounds
- The range doesn’t lie on UTF-8 character boundaries
Sourcepub fn try_slice(&self, range: Range<usize>) -> Option<StrView<'a>>
pub fn try_slice(&self, range: Range<usize>) -> Option<StrView<'a>>
Try to get a subview by byte range.
Returns None if the range is invalid or doesn’t lie on
UTF-8 character boundaries.
Sourcepub unsafe fn slice_unchecked(&self, range: Range<usize>) -> StrView<'a>
pub unsafe fn slice_unchecked(&self, range: Range<usize>) -> StrView<'a>
Get a subview by byte range without bounds checking.
§Safety
The range must be within bounds and on UTF-8 character boundaries.
Sourcepub fn split_once(&self, c: char) -> Option<(StrView<'a>, StrView<'a>)>
pub fn split_once(&self, c: char) -> Option<(StrView<'a>, StrView<'a>)>
Split the view at the first occurrence of a character.
Sourcepub fn strip_prefix(&self, prefix: &str) -> Option<StrView<'a>>
pub fn strip_prefix(&self, prefix: &str) -> Option<StrView<'a>>
Strip a prefix from the view.
Sourcepub fn strip_suffix(&self, suffix: &str) -> Option<StrView<'a>>
pub fn strip_suffix(&self, suffix: &str) -> Option<StrView<'a>>
Strip a suffix from the view.
Sourcepub fn starts_with(&self, prefix: &str) -> bool
pub fn starts_with(&self, prefix: &str) -> bool
Check if the view starts with a prefix.
Trait Implementations§
Source§impl<'a> Ord for StrView<'a>
impl<'a> Ord for StrView<'a>
Source§impl<'a> PartialOrd for StrView<'a>
impl<'a> PartialOrd for StrView<'a>
impl<'a> Copy for StrView<'a>
impl<'a> Eq for StrView<'a>
impl<'a> Send for StrView<'a>
impl<'a> Sync for StrView<'a>
Auto Trait Implementations§
impl<'a> Freeze for StrView<'a>
impl<'a> RefUnwindSafe for StrView<'a>
impl<'a> Unpin for StrView<'a>
impl<'a> UnsafeUnpin for StrView<'a>
impl<'a> UnwindSafe for StrView<'a>
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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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 more