> built-in type
bytes
methods of bytes values
std::bytes creates byte buffers, and byte values have methods for reading and writing integers and floats at byte offsets in either byte order. A bytes value is shared: methods that change it change it in place, and every variable holding it sees the change.
Methods that read or write at an offset raise a runtime error when the range is outside the buffer. Methods that return Result fail with a bytes::Error value instead: OutOfRange (an offset or range is out of bounds), Utf8 (the bytes are not valid UTF-8) or BadHex (a hex string is malformed).
Each read takes a byte offset and returns the value stored there with the width and signedness its name gives. Each write stores value at offset in place. The _le methods use little-endian byte order and the _be methods big-endian; the one-byte methods have no suffix.
They raise a runtime error when the value's bytes don't fit inside the buffer, when offset is negative, when an integer value doesn't fit the width (write_u8(0, 256)), or when an integer width is given a float or a float width an integer.
> methods
The bytes decoded as UTF-8, or Err(bytes::Error::Utf8) when they aren't valid UTF-8.
The offset of the first occurrence of needle, or None. An empty needle is found at 0.
The byte at index as an integer 0–255, or None when the index is negative or past the end.
bytes.is_empty() -> boolWhether the buffer has no bytes.
A new buffer holding a copy of the bytes from start up to, not including, end. A negative index, start after end, or end past the length gives Err(bytes::Error::OutOfRange).
Writes one byte: 0–255 for write_u8, -128–127 for write_i8.
> examples
use std::bytes;
use std::io;
fn main() {
let buf = bytes::zeroed(8);
buf.write_u32_le(0, 0x00905a4d);
io::println("MZ header: {}", buf.to_hex());
io::println("u32 at 0: {}", buf.read_u32_le(0));
}