rustHow to convert Rust bytes to a vector of u8?
To convert Rust bytes to a vector of u8, you can use the to_vec()
method. This method will return a Vec<u8>
from a &[u8]
or &mut [u8]
.
Example code
let bytes = b"Hello World";
let vec = bytes.to_vec();
Output example
[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
Code explanation
let bytes = b"Hello World";
: This line creates a byte array containing the string "Hello World".let vec = bytes.to_vec();
: This line calls theto_vec()
method on thebytes
array, which returns aVec<u8>
containing the same data as thebytes
array.
Helpful links
Related
More of Rust
- How to use regex to match a double quote in Rust?
- How to replace a capture group using Rust regex?
- Bitwise XOR operator usage in Rust
- How to use modifiers in a Rust regex?
- How to map with index in Rust
- How to convert a u8 slice to a hex string in Rust?
- How to calculate the inverse of a matrix in Rust?
- How to use an enum in a Rust HashMap?
- How to match the end of a line in a Rust regex?
- How to create a Rust regex from a string?
See more codes...