O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Byte strings

The third form of strings is not actually a string but rather a stream of bytes. In Rust code, this is the unsigned 8-bit type, encapsulated in either a vector (Vec<u8>) or an array ([u8]). In rather the same way as string slices are usually used by references, so are arrays. So, the latter type is often used as &[u8].

This is how we must work with strings when we're talking with the outside world. All your files are just bytes, just like the data we receive from and send to the internet. This might be a problem since not every array of bytes is valid UTF-8, which is why we need to handle any errors that might arise from the conversion. Recall the preceding conversion:

 let string_from_bytestring: String = String::from_utf8(vec![82, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required