pub struct AsciiStr { /* private fields */ }
Expand description
AsciiStr represents a byte or string slice that only contains ASCII characters.
It wraps an [AsciiChar]
and implements many of str
s methods and traits.
It can be created by a checked conversion from a str
or [u8]
, or borrowed from an
AsciiString
.
Implementations§
Source§impl AsciiStr
impl AsciiStr
Sourcepub fn as_mut_slice(&mut self) -> &mut [AsciiChar]
pub fn as_mut_slice(&mut self) -> &mut [AsciiChar]
Returns the entire string as mutable slice of AsciiChar
s.
Sourcepub fn as_ptr(&self) -> *const AsciiChar
pub fn as_ptr(&self) -> *const AsciiChar
Returns a raw pointer to the AsciiStr
’s buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it
will end up pointing to garbage. Modifying the AsciiStr
may cause it’s buffer to be
reallocated, which would also make any pointers to it invalid.
Sourcepub fn as_mut_ptr(&mut self) -> *mut AsciiChar
pub fn as_mut_ptr(&mut self) -> *mut AsciiChar
Returns an unsafe mutable pointer to the AsciiStr
’s buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it
will end up pointing to garbage. Modifying the AsciiStr
may cause it’s buffer to be
reallocated, which would also make any pointers to it invalid.
Sourcepub fn to_ascii_string(&self) -> AsciiString
pub fn to_ascii_string(&self) -> AsciiString
Copies the content of this AsciiStr
into an owned AsciiString
.
Sourcepub fn from_ascii<B>(bytes: &B) -> Result<&AsciiStr, AsAsciiStrError>
pub fn from_ascii<B>(bytes: &B) -> Result<&AsciiStr, AsAsciiStrError>
Converts anything that can represent a byte slice into an AsciiStr
.
§Examples
let foo = AsciiStr::from_ascii("foo");
let err = AsciiStr::from_ascii("Ŋ");
assert_eq!(foo.unwrap().as_str(), "foo");
assert_eq!(err.unwrap_err().valid_up_to(), 0);
Sourcepub unsafe fn from_ascii_unchecked<B>(bytes: &B) -> &AsciiStr
pub unsafe fn from_ascii_unchecked<B>(bytes: &B) -> &AsciiStr
Converts anything that can be represented as a byte slice to an AsciiStr
without checking
for non-ASCII characters..
§Examples
let foo = unsafe{ AsciiStr::from_ascii_unchecked("foo") };
assert_eq!(foo.as_str(), "foo");
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of characters / bytes in this ASCII sequence.
§Examples
let s = AsciiStr::from_ascii("foo").unwrap();
assert_eq!(s.len(), 3);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the ASCII slice contains zero bytes.
§Examples
let mut empty = AsciiStr::from_ascii("").unwrap();
let mut full = AsciiStr::from_ascii("foo").unwrap();
assert!(empty.is_empty());
assert!(!full.is_empty());
Sourcepub fn chars_mut(&mut self) -> CharsMut<'_>
pub fn chars_mut(&mut self) -> CharsMut<'_>
Returns an iterator over the characters of the AsciiStr
which allows you to modify the
value of each AsciiChar
.
Sourcepub fn split(&self, on: AsciiChar) -> Split<'_>
pub fn split(&self, on: AsciiChar) -> Split<'_>
Returns an iterator over parts of the AsciiStr
separated by a character.
§Examples
let words = AsciiStr::from_ascii("apple banana lemon").unwrap()
.split(AsciiChar::Space)
.map(|a| a.as_str())
.collect::<Vec<_>>();
assert_eq!(words, ["apple", "banana", "lemon"]);
Sourcepub fn lines(&self) -> Lines<'_> ⓘ
pub fn lines(&self) -> Lines<'_> ⓘ
Returns an iterator over the lines of the AsciiStr
, which are themselves AsciiStr
s.
Lines are ended with either LineFeed
(\n
), or CarriageReturn
then LineFeed
(\r\n
).
The final line ending is optional.
Sourcepub fn trim(&self) -> &Self
pub fn trim(&self) -> &Self
Returns an ASCII string slice with leading and trailing whitespace removed.
§Examples
let example = AsciiStr::from_ascii(" \twhite \tspace \t").unwrap();
assert_eq!("white \tspace", example.trim());
Sourcepub fn trim_left(&self) -> &Self
pub fn trim_left(&self) -> &Self
Returns an ASCII string slice with leading whitespace removed.
§Examples
let example = AsciiStr::from_ascii(" \twhite \tspace \t").unwrap();
assert_eq!("white \tspace \t", example.trim_left());
Sourcepub fn trim_right(&self) -> &Self
pub fn trim_right(&self) -> &Self
Returns an ASCII string slice with trailing whitespace removed.
§Examples
let example = AsciiStr::from_ascii(" \twhite \tspace \t").unwrap();
assert_eq!(" \twhite \tspace", example.trim_right());
Sourcepub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
Compares two strings case-insensitively.
Sourcepub fn make_ascii_uppercase(&mut self)
pub fn make_ascii_uppercase(&mut self)
Replaces lowercase letters with their uppercase equivalent.
Sourcepub fn make_ascii_lowercase(&mut self)
pub fn make_ascii_lowercase(&mut self)
Replaces uppercase letters with their lowercase equivalent.
Sourcepub fn to_ascii_uppercase(&self) -> AsciiString
pub fn to_ascii_uppercase(&self) -> AsciiString
Returns a copy of this string where letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’.
Sourcepub fn to_ascii_lowercase(&self) -> AsciiString
pub fn to_ascii_lowercase(&self) -> AsciiString
Returns a copy of this string where letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’.
Trait Implementations§
Source§impl<'a> Add<&'a AsciiStr> for AsciiString
impl<'a> Add<&'a AsciiStr> for AsciiString
Source§type Output = AsciiString
type Output = AsciiString
+
operator.Source§impl<'a> AddAssign<&'a AsciiStr> for AsciiString
impl<'a> AddAssign<&'a AsciiStr> for AsciiString
Source§fn add_assign(&mut self, other: &AsciiStr)
fn add_assign(&mut self, other: &AsciiStr)
+=
operation. Read moreSource§impl AsAsciiStr for AsciiStr
impl AsAsciiStr for AsciiStr
Source§fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>
fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>
Source§unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr
unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr
Source§impl AsMut<AsciiStr> for AsciiString
impl AsMut<AsciiStr> for AsciiString
Source§impl AsMutAsciiStr for AsciiStr
impl AsMutAsciiStr for AsciiStr
Source§fn as_mut_ascii_str(&mut self) -> Result<&mut AsciiStr, AsAsciiStrError>
fn as_mut_ascii_str(&mut self) -> Result<&mut AsciiStr, AsAsciiStrError>
Source§unsafe fn as_mut_ascii_str_unchecked(&mut self) -> &mut AsciiStr
unsafe fn as_mut_ascii_str_unchecked(&mut self) -> &mut AsciiStr
Source§impl AsRef<AsciiStr> for AsciiString
impl AsRef<AsciiStr> for AsciiString
Source§impl AsciiExt for AsciiStr
impl AsciiExt for AsciiStr
Source§type Owned = AsciiString
type Owned = AsciiString
Source§fn is_ascii(&self) -> bool
fn is_ascii(&self) -> bool
Source§fn to_ascii_uppercase(&self) -> AsciiString
fn to_ascii_uppercase(&self) -> AsciiString
Source§fn to_ascii_lowercase(&self) -> AsciiString
fn to_ascii_lowercase(&self) -> AsciiString
Source§fn eq_ignore_ascii_case(&self, other: &Self) -> bool
fn eq_ignore_ascii_case(&self, other: &Self) -> bool
Source§fn make_ascii_uppercase(&mut self)
fn make_ascii_uppercase(&mut self)
Source§fn make_ascii_lowercase(&mut self)
fn make_ascii_lowercase(&mut self)
Source§impl Borrow<AsciiStr> for AsciiString
impl Borrow<AsciiStr> for AsciiString
Source§impl BorrowMut<AsciiStr> for AsciiString
impl BorrowMut<AsciiStr> for AsciiString
Source§fn borrow_mut(&mut self) -> &mut AsciiStr
fn borrow_mut(&mut self) -> &mut AsciiStr
Source§impl<'a> Extend<&'a AsciiStr> for AsciiString
impl<'a> Extend<&'a AsciiStr> for AsciiString
Source§fn extend<I: IntoIterator<Item = &'a AsciiStr>>(&mut self, iterable: I)
fn extend<I: IntoIterator<Item = &'a AsciiStr>>(&mut self, iterable: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'a> From<&'a AsciiStr> for AsciiString
impl<'a> From<&'a AsciiStr> for AsciiString
Source§impl<'a> FromIterator<&'a AsciiStr> for AsciiString
impl<'a> FromIterator<&'a AsciiStr> for AsciiString
Source§fn from_iter<I: IntoIterator<Item = &'a AsciiStr>>(iter: I) -> AsciiString
fn from_iter<I: IntoIterator<Item = &'a AsciiStr>>(iter: I) -> AsciiString
Source§impl<'a> IntoAsciiString for &'a AsciiStr
impl<'a> IntoAsciiString for &'a AsciiStr
Source§unsafe fn into_ascii_string_unchecked(self) -> AsciiString
unsafe fn into_ascii_string_unchecked(self) -> AsciiString
AsciiString
without checking for non-ASCII characters.Source§fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>>
fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>>
AsciiString
.Source§impl<'a> IntoIterator for &'a AsciiStr
impl<'a> IntoIterator for &'a AsciiStr
Source§impl<'a> IntoIterator for &'a mut AsciiStr
impl<'a> IntoIterator for &'a mut AsciiStr
Source§impl<'a> PartialEq<&'a AsciiStr> for AsciiString
impl<'a> PartialEq<&'a AsciiStr> for AsciiString
Source§impl<'a> PartialEq<AsciiString> for &'a AsciiStr
impl<'a> PartialEq<AsciiString> for &'a AsciiStr
Source§fn eq(&self, other: &AsciiString) -> bool
fn eq(&self, other: &AsciiString) -> bool
self
and other
values to be equal, and is used by ==
.Source§fn ne(&self, other: &AsciiString) -> bool
fn ne(&self, other: &AsciiString) -> bool
!=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.