Manu

Rust bytes and String

In Rust a byte refers to 8 bit unsigned integer.

In rust we can represent a byte by u8

let my_byte : u8 = 79;

A string can also be considered as a sequence of bytes. To convert a String to sequence of bytes we can do as below

fn main() {
	let s = "Hello, World";
	let b = s.as_bytes();

	println("{:?}", b);
}

Output

[72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100]

To do the inverse, that is get a String from sequence of bytes we can use String::from_utf8. This returns a Result<String, FromUtf8Error>.

fn main() {
   	let bytes = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100];
   	let s = String::from_utf8(bytes.to_vec()).unwrap();

	println!("{:?}", s); 
}

Output

"Hello, World"

Curiously, I asked around in Rust’s discord why doesn’t String::from_utf8 take an array and only a Vector. Responses I got were