skip to content

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

Terminal window
[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

Terminal window
"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

  • String wraps a Vec<u8> so it will have to be converted to Vec<> either way.
  • Hence, we would need a heap allocation anyways to create a String. So if we take in a Vec we could just take the ownership of that Vec.

Updated on