Chapter 19: Rust Strings

Rust Strings.

This is one of the topics where almost every new Rust learner says: “Wait… why two types? Why not just one like in Python or Java?”

Rust has two main string types:

  • String (owned, growable, heap-allocated)
  • &str (borrowed string slice, usually immutable view)

(and rarely str alone, &mut str, etc.)

The reason for two is Rust’s ownership system — it gives you safety + performance + flexibility without garbage collector or manual memory management. Once it clicks, you’ll love it.

Let me explain like your teacher sitting next to you with code open: slowly, with analogies (biryani plate vs photo of biryani), many runnable examples, tables, and real patterns from 2026 Rust code.

1. The Two Main Types — Quick Overview

Feature String &str (most common)
Ownership Owns the data (heap allocated) Borrows (references) existing data
Mutability Can be changed (grow, shrink, mutate) Immutable view (can’t change the text)
Memory location Heap (dynamic size) Anywhere (static, heap, stack)
When created At runtime (String::from, .to_string()) Compile-time literals or slices
Typical use Build/modify strings, store in structs Function params, read-only views
Example literal String::from(“hello”) “hello” (type: &’static str)
Coercion Can coerce to &str (.as_str()) Cannot become String without allocation

Think of it like this:

  • String = you own a full plate of Hyderabadi biryani — you can add more rice, eat some, give it away (move ownership).
  • &str = a photo of that biryani plate — you can look at it, show to friends, but you can’t change the food in the photo.

2. String Literals — They Are &str (Not String!)

Rust
  • “…” → creates a string literal → stored in binary (static lifetime ‘static)
  • Always &’static str — never String
  • Zero-cost, super efficient

3. Creating String (Owned)

Rust
  • String is growable — has capacity (pre-allocated space) and length
  • Methods: .push(char), .push_str(&str), +=, .clear(), .truncate(), etc.

4. Converting Between Them (Very Common)

Rust

Rule of thumb in 2026 Rust:

  • Prefer &str for function parameters (most flexible)
  • Return String when creating new/modified text
  • Return &str when just viewing existing data

5. Function Parameters — Why &str Wins Almost Always

Rust
  • &str accepts: &str, &String, String (via deref coercion)
  • String only accepts String (moves) or &String if you change signature

→ Use &str in params → more ergonomic, no unnecessary clones

6. Mutating Strings (Only String or &mut str)

Rust

Most mutation happens on String.

7. Common Operations & Gotchas

Rust
  • .len() = bytes
  • .chars().count() = Unicode scalar values
  • No direct indexing text[0] — prevents invalid UTF-8 slices

8. Quick Best Practices (2026 Style)

  • Function params → &str (flexible, zero-cost)
  • Struct fields that own text → String
  • Return new/modified text → String
  • Return view of existing → &str (with lifetime)
  • Avoid &String in params — prefer &str
  • Use .to_string() or format! when building
  • For performance → prefer &str + Cow<str> in advanced cases

Practice Project

Bash

Try in main.rs:

  • Function that takes &str and returns String with ” – Hyderabad” appended
  • Count Unicode chars in a Telugu sentence
  • Build a greeting using String::new() + .push_str + .push emoji

Want me to explain any part deeper?

  • UTF-8 details + why no indexing?
  • String formatting (format!, println! tricks)?
  • Cow<str> (cheap owned-or-borrowed)?
  • Or next topic like structs or ownership deep dive with strings?

Just say — class continues! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *