String Literals and Static Memory

Learn about string literals and static (read-only) memory, which are essential for handling and manipulating strings effectively in Rust.

Now that we’re familiar with how dynamic data types such as vectors, arrays, and string literals are stored in the heap, let’s get a bit more specific when we are dealing with references and strings etc., because it can be quite confusing. So, let’s go back to an example where have strings.

There are two ways to initialize string literals in Rust: one initializes and stores them statically on the stack, and the other dynamically on the heap.

Creating string literals stored on the heap

String literals created on the heap are dynamic and can be altered after they are created. They are of type String.

Get hands-on with 1400+ tech skills courses.