ascii

Struct AsciiStr

Source
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 strs 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

Source

pub fn new<S: AsRef<AsciiStr> + ?Sized>(s: &S) -> &AsciiStr

Coerces into an AsciiStr slice.

Source

pub fn as_str(&self) -> &str

Converts &self to a &str slice.

Source

pub fn as_bytes(&self) -> &[u8]

Converts &self into a byte slice.

Source

pub fn as_slice(&self) -> &[AsciiChar]

Returns the entire string as slice of AsciiChars.

Source

pub fn as_mut_slice(&mut self) -> &mut [AsciiChar]

Returns the entire string as mutable slice of AsciiChars.

Source

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.

Source

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.

Source

pub fn to_ascii_string(&self) -> AsciiString

Copies the content of this AsciiStr into an owned AsciiString.

Source

pub fn from_ascii<B>(bytes: &B) -> Result<&AsciiStr, AsAsciiStrError>
where B: AsRef<[u8]> + ?Sized,

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);
Source

pub unsafe fn from_ascii_unchecked<B>(bytes: &B) -> &AsciiStr
where B: AsRef<[u8]> + ?Sized,

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");
Source

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);
Source

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());
Source

pub fn chars(&self) -> Chars<'_>

Returns an iterator over the characters of the AsciiStr.

Source

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.

Source

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"]);
Source

pub fn lines(&self) -> Lines<'_>

Returns an iterator over the lines of the AsciiStr, which are themselves AsciiStrs.

Lines are ended with either LineFeed (\n), or CarriageReturn then LineFeed (\r\n).

The final line ending is optional.

Source

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());
Source

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());
Source

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());
Source

pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool

Compares two strings case-insensitively.

Source

pub fn make_ascii_uppercase(&mut self)

Replaces lowercase letters with their uppercase equivalent.

Source

pub fn make_ascii_lowercase(&mut self)

Replaces uppercase letters with their lowercase equivalent.

Source

pub fn to_ascii_uppercase(&self) -> AsciiString

Returns a copy of this string where letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’.

Source

pub fn to_ascii_lowercase(&self) -> AsciiString

Returns a copy of this string where letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’.

Source

pub fn first(&self) -> Option<AsciiChar>

Returns the first character if the string is not empty.

Source

pub fn last(&self) -> Option<AsciiChar>

Returns the last character if the string is not empty.

Trait Implementations§

Source§

impl<'a> Add<&'a AsciiStr> for AsciiString

Source§

type Output = AsciiString

The resulting type after applying the + operator.
Source§

fn add(self, other: &AsciiStr) -> AsciiString

Performs the + operation. Read more
Source§

impl<'a> AddAssign<&'a AsciiStr> for AsciiString

Source§

fn add_assign(&mut self, other: &AsciiStr)

Performs the += operation. Read more
Source§

impl AsAsciiStr for AsciiStr

Source§

fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>

Convert to an ASCII slice.
Source§

unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr

Convert to an ASCII slice without checking for non-ASCII characters.
Source§

impl AsMut<[AsciiChar]> for AsciiStr

Source§

fn as_mut(&mut self) -> &mut [AsciiChar]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<AsciiStr> for [AsciiChar]

Source§

fn as_mut(&mut self) -> &mut AsciiStr

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<AsciiStr> for AsciiString

Source§

fn as_mut(&mut self) -> &mut AsciiStr

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMutAsciiStr for AsciiStr

Source§

fn as_mut_ascii_str(&mut self) -> Result<&mut AsciiStr, AsAsciiStrError>

Convert to a mutable ASCII slice.
Source§

unsafe fn as_mut_ascii_str_unchecked(&mut self) -> &mut AsciiStr

Convert to a mutable ASCII slice without checking for non-ASCII characters.
Source§

impl AsRef<[AsciiChar]> for AsciiStr

Source§

fn as_ref(&self) -> &[AsciiChar]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[u8]> for AsciiStr

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<AsciiStr> for [AsciiChar]

Source§

fn as_ref(&self) -> &AsciiStr

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<AsciiStr> for AsciiString

Source§

fn as_ref(&self) -> &AsciiStr

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<str> for AsciiStr

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsciiExt for AsciiStr

Source§

type Owned = AsciiString

👎Deprecated since 1.26.0: use inherent methods instead
Container type for copied ASCII characters.
Source§

fn is_ascii(&self) -> bool

👎Deprecated since 1.26.0: use inherent methods instead
Checks if the value is within the ASCII range. Read more
Source§

fn to_ascii_uppercase(&self) -> AsciiString

👎Deprecated since 1.26.0: use inherent methods instead
Makes a copy of the value in its ASCII upper case equivalent. Read more
Source§

fn to_ascii_lowercase(&self) -> AsciiString

👎Deprecated since 1.26.0: use inherent methods instead
Makes a copy of the value in its ASCII lower case equivalent. Read more
Source§

fn eq_ignore_ascii_case(&self, other: &Self) -> bool

👎Deprecated since 1.26.0: use inherent methods instead
Checks that two values are an ASCII case-insensitive match. Read more
Source§

fn make_ascii_uppercase(&mut self)

👎Deprecated since 1.26.0: use inherent methods instead
Converts this type to its ASCII upper case equivalent in-place. Read more
Source§

fn make_ascii_lowercase(&mut self)

👎Deprecated since 1.26.0: use inherent methods instead
Converts this type to its ASCII lower case equivalent in-place. Read more
Source§

impl Borrow<AsciiStr> for AsciiString

Source§

fn borrow(&self) -> &AsciiStr

Immutably borrows from an owned value. Read more
Source§

impl BorrowMut<AsciiStr> for AsciiString

Source§

fn borrow_mut(&mut self) -> &mut AsciiStr

Mutably borrows from an owned value. Read more
Source§

impl Debug for AsciiStr

Source§

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

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

impl Default for &'static AsciiStr

Source§

fn default() -> &'static AsciiStr

Returns the “default value” for a type. Read more
Source§

impl Display for AsciiStr

Source§

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

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

impl<'a> Extend<&'a AsciiStr> for AsciiString

Source§

fn extend<I: IntoIterator<Item = &'a AsciiStr>>(&mut self, iterable: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a> From<&'a [AsciiChar]> for &'a AsciiStr

Source§

fn from(slice: &[AsciiChar]) -> &AsciiStr

Converts to this type from the input type.
Source§

impl<'a> From<&'a AsciiStr> for &'a [AsciiChar]

Source§

fn from(astr: &AsciiStr) -> &[AsciiChar]

Converts to this type from the input type.
Source§

impl<'a> From<&'a AsciiStr> for &'a [u8]

Source§

fn from(astr: &AsciiStr) -> &[u8]

Converts to this type from the input type.
Source§

impl<'a> From<&'a AsciiStr> for &'a str

Source§

fn from(astr: &AsciiStr) -> &str

Converts to this type from the input type.
Source§

impl<'a> From<&'a AsciiStr> for AsciiString

Source§

fn from(s: &'a AsciiStr) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a AsciiStr> for Cow<'a, AsciiStr>

Source§

fn from(s: &'a AsciiStr) -> Cow<'a, AsciiStr>

Converts to this type from the input type.
Source§

impl<'a> From<&'a mut [AsciiChar]> for &'a mut AsciiStr

Source§

fn from(slice: &mut [AsciiChar]) -> &mut AsciiStr

Converts to this type from the input type.
Source§

impl<'a> From<&'a mut AsciiStr> for &'a mut [AsciiChar]

Source§

fn from(astr: &mut AsciiStr) -> &mut [AsciiChar]

Converts to this type from the input type.
Source§

impl From<Box<[AsciiChar]>> for Box<AsciiStr>

Source§

fn from(owned: Box<[AsciiChar]>) -> Box<AsciiStr>

Converts to this type from the input type.
Source§

impl<'a> FromIterator<&'a AsciiStr> for AsciiString

Source§

fn from_iter<I: IntoIterator<Item = &'a AsciiStr>>(iter: I) -> AsciiString

Creates a value from an iterator. Read more
Source§

impl Hash for AsciiStr

Source§

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

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

impl Index<Range<usize>> for AsciiStr

Source§

type Output = AsciiStr

The returned type after indexing.
Source§

fn index(&self, index: Range<usize>) -> &AsciiStr

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeFrom<usize>> for AsciiStr

Source§

type Output = AsciiStr

The returned type after indexing.
Source§

fn index(&self, index: RangeFrom<usize>) -> &AsciiStr

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeFull> for AsciiStr

Source§

type Output = AsciiStr

The returned type after indexing.
Source§

fn index(&self, index: RangeFull) -> &AsciiStr

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeTo<usize>> for AsciiStr

Source§

type Output = AsciiStr

The returned type after indexing.
Source§

fn index(&self, index: RangeTo<usize>) -> &AsciiStr

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<usize> for AsciiStr

Source§

type Output = AsciiChar

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &AsciiChar

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<Range<usize>> for AsciiStr

Source§

fn index_mut(&mut self, index: Range<usize>) -> &mut AsciiStr

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<RangeFrom<usize>> for AsciiStr

Source§

fn index_mut(&mut self, index: RangeFrom<usize>) -> &mut AsciiStr

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<RangeFull> for AsciiStr

Source§

fn index_mut(&mut self, index: RangeFull) -> &mut AsciiStr

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<RangeTo<usize>> for AsciiStr

Source§

fn index_mut(&mut self, index: RangeTo<usize>) -> &mut AsciiStr

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for AsciiStr

Source§

fn index_mut(&mut self, index: usize) -> &mut AsciiChar

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a> IntoAsciiString for &'a AsciiStr

Source§

unsafe fn into_ascii_string_unchecked(self) -> AsciiString

Convert to AsciiString without checking for non-ASCII characters.
Source§

fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>>

Convert to AsciiString.
Source§

impl<'a> IntoIterator for &'a AsciiStr

Source§

type Item = &'a AsciiChar

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, AsciiChar>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut AsciiStr

Source§

type Item = &'a mut AsciiChar

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, AsciiChar>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Ord for AsciiStr

Source§

fn cmp(&self, other: &AsciiStr) -> Ordering

This method returns an Ordering between self and other. Read more
Source§

impl<'a> PartialEq<&'a AsciiStr> for AsciiString

Source§

fn eq(&self, other: &&'a AsciiStr) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &&'a AsciiStr) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialEq<&'a AsciiStr> for String

Source§

fn eq(&self, other: &&'a AsciiStr) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &&'a AsciiStr) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<[AsciiChar]> for AsciiStr

Source§

fn eq(&self, other: &[AsciiChar]) -> 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 PartialEq<[u8]> for AsciiStr

Source§

fn eq(&self, other: &[u8]) -> 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 PartialEq<AsciiStr> for [AsciiChar]

Source§

fn eq(&self, other: &AsciiStr) -> 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 PartialEq<AsciiStr> for [u8]

Source§

fn eq(&self, other: &AsciiStr) -> 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 PartialEq<AsciiStr> for str

Source§

fn eq(&self, other: &AsciiStr) -> 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<'a> PartialEq<AsciiString> for &'a AsciiStr

Source§

fn eq(&self, other: &AsciiString) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &AsciiString) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialEq<String> for &'a AsciiStr

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &String) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for AsciiStr

Source§

fn eq(&self, other: &str) -> 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 PartialEq for AsciiStr

Source§

fn eq(&self, other: &AsciiStr) -> 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 PartialOrd for AsciiStr

Source§

fn partial_cmp(&self, other: &AsciiStr) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl ToOwned for AsciiStr

Source§

type Owned = AsciiString

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> AsciiString

Creates owned data from borrowed data, usually by cloning. Read more
1.63.0 · Source§

fn clone_into(&self, target: &mut Self::Owned)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl Eq for AsciiStr

Source§

impl StructuralPartialEq for AsciiStr

Auto Trait Implementations§

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

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more