It is not possible to transform a String into a byte object without doing a conversion. This is because a Rust String is a UTF-8 encoded Unicode string, which is different from a bytes object that could represent any binary data.
To convert a String to a Bytes object, you can use the as_bytes()
method provided by the String
type. For example:
let my_string = String::from("hello");
let my_bytes = my_string.as_bytes();
This will create a new byte array with the same data as the original string. Note that this operation creates a new object, so technically it is a conversion.
Asked: 2023-06-17 11:46:01 +0000
Seen: 18 times
Last updated: Jun 17 '23