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

The String type

OK, let's take a look at the higher level String type. Like the string slice, its contents are guaranteed to be Unicode. Unlike string slices, String is mutable and growable, it can be created during runtime, and it actually holds the data inside. Unfortunately, these great features have a downside. The String type is not of zero cost: it needs to be allocated in the heap and possibly reallocated when it grows. Heap allocation is a relatively expensive operation, but, fortunately for most applications, this cost is negligible. We'll cover memory allocation more thoroughly in Chapter 6, Memory, Lifetimes, and Borrowing.

A String type can be cast into &str rather transparently (as in the example we just saw) but not vice versa. ...

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