pub trait AsAsciiStr {
    fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
   where
        R: SliceIndex<[Self::Inner], Output = [Self::Inner]>
; unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr; fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError> { ... } fn get_ascii(&self, index: usize) -> Option<AsciiChar> { ... } }
Expand description

Convert slices of bytes or AsciiChar to AsciiStr.

Required Methods

Convert a subslice to an ASCII slice.

Errors

Returns Err if the range is out of bounds or if not all bytes in the slice are ASCII. The value in the error will be the index of the first non-ASCII byte or the end of the slice.

Examples
use ascii::AsAsciiStr;
assert!("'zoä'".slice_ascii(..3).is_ok());
assert!("'zoä'".slice_ascii(0..4).is_err());
assert!("'zoä'".slice_ascii(5..=5).is_ok());
assert!("'zoä'".slice_ascii(4..).is_err());
assert!(b"\r\n".slice_ascii(..).is_ok());

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

Safety

Calling this function when self contains non-ascii characters is undefined behavior.

Examples

Provided Methods

Convert to an ASCII slice.

Errors

Returns Err if not all bytes are valid ascii values.

Example
use ascii::{AsAsciiStr, AsciiChar};
assert!("ASCII".as_ascii_str().is_ok());
assert!(b"\r\n".as_ascii_str().is_ok());
assert!("'zoä'".as_ascii_str().is_err());
assert!(b"\xff".as_ascii_str().is_err());
assert!([AsciiChar::C][..].as_ascii_str().is_ok()); // infallible

Get a single ASCII character from the slice.

Returns None if the index is out of bounds or the byte is not ASCII.

Examples
use ascii::{AsAsciiStr, AsciiChar};
assert_eq!("'zoä'".get_ascii(4), None);
assert_eq!("'zoä'".get_ascii(5), Some(AsciiChar::Apostrophe));
assert_eq!("'zoä'".get_ascii(6), None);

Implementations on Foreign Types

Note that the trailing null byte will be removed in the conversion.

Implementors