Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.