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

String types

Rust has two types of strings: string slices (str) and String. They are both guaranteed by runtime checks to be valid Unicode strings, and they are both internally coded as UTF-8. There are no separate non-Unicode character or string types; the primitive type u8 is used for streams of bytes that may or may not be Unicode.

Why the two types? They exist mostly because of Rust's memory management and its philosophy of zero runtime cost. Passing string slices around your program is nearly free: it incurs nearly no allocation costs and no copying of memory. Unfortunately, nothing is actually free, and in this case, it means that you, the programmer, will need to pay some of that price. The concept of string slices is probably new ...

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