#[repr(transparent)]pub struct ByteStr(pub [u8]);bstr #134915)Expand description
A wrapper for &[u8] representing a human-readable string that’s conventionally, but not
always, UTF-8.
Unlike &str, this type permits non-UTF-8 contents, making it suitable for user input,
non-native filenames (as Path only supports native filenames), and other applications that
need to round-trip whatever data the user provides.
For an owned, growable byte string buffer, use
ByteString.
ByteStr implements Deref to [u8], so all methods available on [u8] are available on
ByteStr.
§Representation
A &ByteStr has the same representation as a &str. That is, a &ByteStr is a wide pointer
which includes a pointer to some bytes and a length.
§Trait implementations
The ByteStr type has a number of trait implementations, and in particular, defines equality
and comparisons between &ByteStr, &str, and &[u8], for convenience.
The Debug implementation for ByteStr shows its bytes as a normal string, with invalid UTF-8
presented as hex escape sequences.
The Display implementation behaves as if the ByteStr were first lossily converted to a
str, with invalid UTF-8 presented as the Unicode replacement character (�).
Tuple Fields§
§0: [u8]bstr #134915)Implementations§
Source§impl ByteStr
impl ByteStr
Sourcepub const fn new<B>(bytes: &B) -> &ByteStr
🔬This is a nightly-only experimental API. (bstr #134915)
pub const fn new<B>(bytes: &B) -> &ByteStr
bstr #134915)Creates a ByteStr slice from anything that can be converted to a byte slice.
This is a zero-cost conversion.
§Example
You can create a ByteStr from a byte array, a byte slice or a string slice:
Sourcepub const fn as_byte_str(&self) -> &ByteStr
🔬This is a nightly-only experimental API. (bstr #134915)
pub const fn as_byte_str(&self) -> &ByteStr
bstr #134915)Returns the same string as &ByteStr.
This method is redundant when used directly on &ByteStr, but
it helps dereferencing other “container” types,
for example Box<ByteStr> or Arc<ByteStr>.
Sourcepub const fn as_mut_byte_str(&mut self) -> &mut ByteStr
🔬This is a nightly-only experimental API. (bstr #134915)
pub const fn as_mut_byte_str(&mut self) -> &mut ByteStr
bstr #134915)Returns the same string as &mut ByteStr.
This method is redundant when used directly on &mut ByteStr, but
it helps dereferencing other “container” types,
for example Box<ByteStr> or MutexGuard<ByteStr>.
Methods from Deref<Target = [u8]>§
1.23.0 · Sourcepub fn is_ascii(&self) -> bool
pub fn is_ascii(&self) -> bool
Checks if all bytes in this slice are within the ASCII range.
An empty slice returns true.
Sourcepub fn as_ascii(&self) -> Option<&[AsciiChar]>
🔬This is a nightly-only experimental API. (ascii_char #110998)
pub fn as_ascii(&self) -> Option<&[AsciiChar]>
ascii_char #110998)If this slice is_ascii, returns it as a slice of
ASCII characters, otherwise returns None.
Sourcepub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar]
🔬This is a nightly-only experimental API. (ascii_char #110998)
pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar]
ascii_char #110998)Converts this slice of bytes into a slice of ASCII characters, without checking whether they’re valid.
§Safety
Every byte in the slice must be in 0..=127, or else this is UB.
1.23.0 · Sourcepub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool
pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool
Checks that two slices are an ASCII case-insensitive match.
Same as to_ascii_lowercase(a) == to_ascii_lowercase(b),
but without allocating and copying temporaries.
1.23.0 · Sourcepub fn make_ascii_uppercase(&mut self)
pub fn make_ascii_uppercase(&mut self)
Converts this slice to its ASCII upper case equivalent in-place.
ASCII letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’, but non-ASCII letters are unchanged.
To return a new uppercased value without modifying the existing one, use
to_ascii_uppercase.
1.23.0 · Sourcepub fn make_ascii_lowercase(&mut self)
pub fn make_ascii_lowercase(&mut self)
Converts this slice to its ASCII lower case equivalent in-place.
ASCII letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’, but non-ASCII letters are unchanged.
To return a new lowercased value without modifying the existing one, use
to_ascii_lowercase.
1.60.0 · Sourcepub fn escape_ascii(&self) -> EscapeAscii<'_> ⓘ
pub fn escape_ascii(&self) -> EscapeAscii<'_> ⓘ
Returns an iterator that produces an escaped version of this slice, treating it as an ASCII string.
§Examples
1.80.0 · Sourcepub fn trim_ascii_start(&self) -> &[u8] ⓘ
pub fn trim_ascii_start(&self) -> &[u8] ⓘ
Returns a byte slice with leading ASCII whitespace bytes removed.
‘Whitespace’ refers to the definition used by
u8::is_ascii_whitespace. Importantly, this definition excludes
the \0x0B byte even though it has the Unicode White_Space property
and is removed by str::trim_start.
§Examples
1.80.0 · Sourcepub fn trim_ascii_end(&self) -> &[u8] ⓘ
pub fn trim_ascii_end(&self) -> &[u8] ⓘ
Returns a byte slice with trailing ASCII whitespace bytes removed.
‘Whitespace’ refers to the definition used by
u8::is_ascii_whitespace. Importantly, this definition excludes
the \0x0B byte even though it has the Unicode White_Space property
and is removed by str::trim_end.
§Examples
1.80.0 · Sourcepub fn trim_ascii(&self) -> &[u8] ⓘ
pub fn trim_ascii(&self) -> &[u8] ⓘ
Returns a byte slice with leading and trailing ASCII whitespace bytes removed.
‘Whitespace’ refers to the definition used by
u8::is_ascii_whitespace. Importantly, this definition excludes
the \0x0B byte even though it has the Unicode White_Space property
and is removed by str::trim.
§Examples
1.0.0 · Sourcepub fn first(&self) -> Option<&T>
pub fn first(&self) -> Option<&T>
Returns the first element of the slice, or None if it is empty.
§Examples
1.0.0 · Sourcepub fn first_mut(&mut self) -> Option<&mut T>
pub fn first_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the first element of the slice, or None if it is empty.
§Examples
1.5.0 · Sourcepub fn split_first(&self) -> Option<(&T, &[T])>
pub fn split_first(&self) -> Option<(&T, &[T])>
Returns the first and all the rest of the elements of the slice, or None if it is empty.
§Examples
1.5.0 · Sourcepub fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])>
pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])>
Returns the first and all the rest of the elements of the slice, or None if it is empty.
§Examples
1.5.0 · Sourcepub fn split_last(&self) -> Option<(&T, &[T])>
pub fn split_last(&self) -> Option<(&T, &[T])>
Returns the last and all the rest of the elements of the slice, or None if it is empty.