Struct ::std::bytes::Bytes

A vector of bytes.

Methods

fn new() -> Bytes

Construct a new byte array.

Examples

let bytes = Bytes::new();
assert_eq!(bytes, b"");
fn with_capacity(capacity: i64) -> Bytes

Construct a byte array with the given preallocated capacity.

Examples

let bytes = Bytes::with_capacity(32);
assert_eq!(bytes, b"");
bytes.extend(b"abcd");
assert_eq!(bytes, b"abcd");
fn from_vec(bytes: Vec) -> Bytes

Convert a byte array into bytes.

Examples

let bytes = Bytes::from_vec([b'a', b'b', b'c', b'd']);
assert_eq!(bytes, b"abcd");
fn into_vec(self) -> Vec

Convert the byte array into a vector of bytes.

Examples

let bytes = b"abcd";
assert_eq!([b'a', b'b', b'c', b'd'], bytes.into_vec());

assert!(!is_readable(bytes));
fn as_vec(self) -> Vec

Convert the byte array into a vector of bytes without consuming it.

Examples

let bytes = b"abcd";
assert_eq!([b'a', b'b', b'c', b'd'], bytes.as_vec());

assert!(is_readable(bytes));
fn extend(self, other: Bytes) -> Tuple

Extend these bytes with another collection of bytes.

Examples

let bytes = b"abcd";
bytes.extend(b"efgh");
assert_eq!(bytes, b"abcdefgh");

Extend this bytes collection with a string.

Examples

let bytes = b"abcd";
bytes.extend_str("efgh");
assert_eq!(bytes, b"abcdefgh");
fn pop(self) -> Option

Pop the last byte.

Examples

let bytes = b"abcd";
assert_eq!(bytes.pop(), Some(b'd'));
assert_eq!(bytes, b"abc");
fn last(self) -> Option

Get the last byte.

Examples

let bytes = b"abcd";
assert_eq!(bytes.last(), Some(b'd'));
fn len(self) -> i64

Get the length of the bytes collection.

Examples

let bytes = Bytes::new();
assert_eq!(bytes.len(), 0);
bytes.extend(b"abcd");
assert_eq!(bytes.len(), 4);
fn is_empty(self) -> bool

Test if the collection is empty.

Examples

let bytes = Bytes::new();
assert!(bytes.is_empty());
fn capacity(self) -> i64

Returns the total number of elements the vector can hold without reallocating.

Examples

let bytes = Bytes::with_capacity(10);
bytes.extend(b"abc");
assert!(bytes.capacity() >= 10);
fn clear(self) -> Tuple

Clears the vector, removing all values.

Note that this method has no effect on the allocated capacity of the vector.

Examples

let bytes = b"abc";
bytes.clear();
assert!(bytes.is_empty());
fn reserve(self, additional: i64) -> Tuple

Reserves capacity for at least additional more elements to be inserted in the given Bytes. The collection may reserve more space to speculatively avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

Panics

Panics if the new capacity exceeds isize::MAX bytes.

Examples

let vec = b"a";
vec.reserve(10);
assert!(vec.capacity() >= 11);
fn reserve_exact(self, additional: i64) -> Tuple

Reserves the minimum capacity for at least additional more elements to be inserted in the given Bytes. Unlike reserve, this will not deliberately over-allocate to speculatively avoid frequent allocations. After calling reserve_exact, capacity will be greater than or equal to self.len() + additional. Does nothing if the capacity is already sufficient.

Note that the allocator may give the collection more space than it requests. Therefore, capacity can not be relied upon to be precisely minimal. Prefer reserve if future insertions are expected.

Panics

Panics if the new capacity exceeds isize::MAX bytes.

Examples

let vec = b"a";
vec.reserve_exact(10);
assert!(vec.capacity() >= 11);
fn clone(self) -> Bytes

Clone the byte array.

Examples

let a = b"hello world";
let b = a.clone();

a.extend(b"!");

assert_eq!(a, b"hello world!");
assert_eq!(b, b"hello world");

Shrinks the capacity of the byte array as much as possible.

It will drop down as close as possible to the length but the allocator may still inform the byte array that there is space for a few more elements.

Examples

let bytes = Bytes::with_capacity(10);
bytes.extend(b"abc");
assert!(bytes.capacity() >= 10);
bytes.shrink_to_fit();
assert!(bytes.capacity() >= 3);