String vs str in Rust: Main Differences with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

String vs str in Rust: Why Beginners Get Confused and How to Understand It Once and For All

\n\n

If you're just starting to learn Rust, you've probably already encountered a situation: the compiler complains, you try to pass a String where &str is expected, or vice versa. Sound familiar? This is one of the most popular questions on Stack Overflow (thousands of views!), and for good reason — the topic is truly fundamental for understanding memory management in Rust.

\n\n

In this article, we'll break down the differences between String and &str, when to use each type, and how to avoid typical mistakes. Let's go!

\n\n

Short Answer for Those in a Hurry

\n\n

If you need it right now — remember the main points:

\n\n
    \n
  • String is an owning, mutable, dynamic data type that is stored on the heap.
  • \n
  • &str is an immutable string slice that references data owned by someone else. It is most commonly encountered as &str.
  • \n
\n\n

Now let's dig deeper.

\n\n

What is String in Rust?

\n\n

String is a type that owns its data. When you create a String, memory for it is allocated on the heap, and the string can grow, shrink, or be modified.

\n\n
let mut s = String::from("Hello");\ns.push_str(", world!");\nprintln!("{}", s); // Output: Hello, world!
\n\n

Here, s is the owner of the string. When s goes out of scope, the memory is automatically freed (thanks to RAII — Resource Acquisition Is Initialization).

\n\n

Key Characteristics of String:

\n
    \n
  • Owns the data (owning type)
  • \n
  • Mutable
  • \n
  • Stored on the heap
  • \n
  • Can be created from a string literal: String::from("text")
  • \n
  • Supports concatenation, insertion, and deletion of characters
  • \n
\n\n

What is &str in Rust?

\n\n

&str (pronounced "string slice") is a reference to a sequence of UTF-8 characters that belongs to someone else. By itself, &str is simply a pointer to the data and a length.

\n\n
let greeting: &str = "Hello";\nprintln!("{}", greeting);
\n\n

In this example, greeting is a reference to a string literal that is stored directly in the program's binary file (in the read-only memory segment). greeting does not own this data — it just looks at it.

\n\n

Key Characteristics of &str:

\n
    \n
  • Does not own the data (borrowed type)
  • \n
  • Immutable
  • \n
  • Stored either in the binary (string literals) or on the heap (as a slice from a String)
  • \n
  • Lightweight — consists of two fields: pointer + length (16 bytes on a 64-bit system)
  • \n
  • Ideal for passing to functions when you don't need to modify the string
  • \n
\n\n

Key Differences Between String and &str (Table)

\n\n

Let's summarize everything in a clear table:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
CharacteristicString&str
OwnershipOwns the dataBorrows the data
MutabilityYes (mut)No
Storage LocationHeapHeap, stack, or binary
Size on Stack24 bytes (ptr + len + cap)16 bytes (ptr + len)
CreationString::from("...")"..." or &s[..]
Primary UseWhen you need to modify or own

Blogs

Book Recommendations