juniorRust

Difference between String and &str in Rust

Updated May 24, 2026

Short answer

String is owned and mutable; &str is an immutable string slice.

Deep explanation

String stores heap-allocated data, while &str is a borrowed view into string data, often stored in binary or heap.

Real-world example

Parsing input vs storing persistent text.

Common mistakes

  • Confusing ownership and borrowing semantics.

Follow-up questions

  • Can &str outlive String?
  • When to use String?

More Rust interview questions

View all →