rustHow to join bytes in Rust
Joining bytes in Rust can be done using the concat()
method from the std::vec
module. This method takes a vector of bytes and returns a single byte vector containing all the bytes from the original vector.
Code example:
use std::vec;
let bytes1 = vec![0x01, 0x02, 0x03];
let bytes2 = vec![0x04, 0x05, 0x06];
let joined_bytes = vec::concat(vec![bytes1, bytes2]);
println!("{:?}", joined_bytes);
Output
[1, 2, 3, 4, 5, 6]
Explanation:
use std::vec
: imports thevec
module from thestd
librarylet bytes1 = vec![0x01, 0x02, 0x03]
: creates a vector of bytes containing the values0x01
,0x02
, and0x03
let bytes2 = vec![0x04, 0x05, 0x06]
: creates a vector of bytes containing the values0x04
,0x05
, and0x06
let joined_bytes = vec::concat(vec![bytes1, bytes2])
: calls theconcat()
method from thevec
module, passing in a vector containing the two byte vectorsbytes1
andbytes2
println!("{:?}", joined_bytes)
: prints the joined bytes vector
Helpful links:
More of Rust
- Hashshet example in Rust
- How to convert Rust bytes to hex?
- How to convert a Rust HashMap to a JSON string?
- How to convert a Rust HashMap to JSON?
- How to get the last element of a Rust slice?
- How to use non-capturing groups in Rust regex?
- How to use groups in a Rust regex?
- How to match the end of a line in a Rust regex?
- How to escape dots with regex in Rust?
- How to use regex to match a group in Rust?
See more codes...