Skip to main content

ByteStr

Struct ByteStr 

Source
#[repr(transparent)]
pub struct ByteStr(pub [u8]);
🔬This is a nightly-only experimental API. (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]
🔬This is a nightly-only experimental API. (bstr #134915)

Implementations§

Source§

impl ByteStr

Source

pub const fn new<B>(bytes: &B) -> &ByteStr
where B: AsRef<[u8]> + ?Sized,

🔬This is a nightly-only experimental API. (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:

let a = ByteStr::new(b"abc");
let b = ByteStr::new(&b"abc"[..]);
let c = ByteStr::new("abc");

assert_eq!(a, b);
assert_eq!(a, c);
Source

pub const fn as_byte_str(&self) -> &ByteStr

🔬This is a nightly-only experimental API. (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>.

Source

pub const fn as_mut_byte_str(&mut self) -> &mut ByteStr

🔬This is a nightly-only experimental API. (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 · Source

pub fn is_ascii(&self) -> bool

Checks if all bytes in this slice are within the ASCII range.

An empty slice returns true.

Source

pub fn as_ascii(&self) -> Option<&[AsciiChar]>

🔬This is a nightly-only experimental API. (ascii_char #110998)

If this slice is_ascii, returns it as a slice of ASCII characters, otherwise returns None.

Source

pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar]

🔬This is a nightly-only experimental API. (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 · Source

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

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

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

pub fn escape_ascii(&self) -> EscapeAscii<'_> ⓘ

Returns an iterator that produces an escaped version of this slice, treating it as an ASCII string.

§Examples
let s = b"0\t\r\n'\"\\\x9d";
let escaped = s.escape_ascii().to_string();
assert_eq!(escaped, "0\\t\\r\\n\\'\\\"\\\\\\x9d");
1.80.0 · Source

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
assert_eq!(b" \t hello world\n".trim_ascii_start(), b"hello world\n");
assert_eq!(b"  ".trim_ascii_start(), b"");
assert_eq!(b"".trim_ascii_start(), b"");
1.80.0 · Source

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
assert_eq!(b"\r hello world\n ".trim_ascii_end(), b"\r hello world");
assert_eq!(b"  ".trim_ascii_end(), b"");
assert_eq!(b"".trim_ascii_end(), b"");
1.80.0 · Source

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
assert_eq!(b"\r hello world\n ".trim_ascii(), b"hello world");
assert_eq!(b"  ".trim_ascii(), b"");
assert_eq!(b"".trim_ascii(), b"");
1.0.0 · Source

pub fn len(&self) -> usize

Returns the number of elements in the slice.

§Examples
let a = [1, 2, 3];
assert_eq!(a.len(), 3);
1.0.0 · Source

pub fn is_empty(&self) -> bool

Returns true if the slice has a length of 0.

§Examples
let a = [1, 2, 3];
assert!(!a.is_empty());

let b: &[i32] = &[];
assert!(b.is_empty());
1.0.0 · Source

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

Returns the first element of the slice, or None if it is empty.

§Examples
let v = [10, 40, 30];
assert_eq!(Some(&10), v.first());

let w: &[i32] = &[];
assert_eq!(None, w.first());
1.0.0 · Source

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
let x = &mut [0, 1, 2];

if let Some(first) = x.first_mut() {
    *first = 5;
}
assert_eq!(x, &[5, 1, 2]);

let y: &mut [i32] = &mut [];
assert_eq!(None, y.first_mut());
1.5.0 · Source

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
let x = &[0, 1, 2];

if let Some((first, elements)) = x.split_first() {
    assert_eq!(first, &0);
    assert_eq!(elements, &[1, 2]);
}
1.5.0 · Source

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
let x = &mut [0, 1, 2];

if let Some((first, elements)) = x.split_first_mut() {
    *first = 3;
    elements[0] = 4;
    elements[1] = 5;
}
assert_eq!(x, &[3, 4, 5]);
1.5.0 · Source

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.

§Examples
let x = &[0, 1, 2];

if let Some((last, elements)) = x.split_last() {
    assert_eq!(last, &2);
    assert_eq!(elements, &[0, 1]);
}
1.5.0 · Source

pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut [