Strings: The "UTF-8 Narrative"
In JavaScript, a “string” is just a string. Whether it’s a literal or a variable, it behaves the same. In Rust, Strings are much more complex because they are built on the Ownership and Borrowing.
Think of Rust Strings as a Story of Two Types: One that you own and can grow (String), and one that you merely look at (&str).
🏗️ The 3-Step Narrative
1. String: The “Owner” (Heap Data)
A String is like a JS string variable that you’ve declared with let. It is stored on the heap, it can grow in size, and you own it.
let mut s = String::from("hello"); // 📦 Creating a heap owners.push_str(", world!"); // 🪄 Modifying the owned data2. &str: The “Lookout” (String Slice)
A &str (pronounced “string slice”) is an immutable reference to some UTF-8 data. It’s like a Window Looking at a part of a String or a hardcoded literal.
let s = String::from("hello world");let hello = &s[0..5]; // 🪟 A window looking at "hello"NOTE: The Magic
Because a
&stris just a window, it doesn’t “own” the data. This makes passing it around incredibly fast because you are just passing a memory address, not copying the text.
3. The Conversion Contract
You frequently need to turn one into the other.
&str➔String: Use.to_string()orString::from().String➔&str: Just use the&symbol (Rust’s “Deref Coercion” does this automatically for you in most cases).
🔬 From the Official Book
UTF-8 Everywhere
Every String in Rust is guaranteed to be valid UTF-8. This means you can’t index into a string using s[0] like in JS, because some Unicode characters occupy multiple bytes.
let hello = "नमस्ते"; // ❌ s[0] would return a garbage byte, not a character.To get characters, you must use the .chars() iterator: for c in hello.chars() { ... }.
💡 What Strings Unlock
- International Support: By enforcing UTF-8, Rust ensures your application handles emojis, accented characters, and multiple languages perfectly.
- Zero-Copy Performance: Using
&strallows you to process massive documents by just passing “windows” (slices) around instead of duplicating memory. - Strict Safety: You will never have a “string out of bounds” or “invalid encoding” crash in production because the compiler and standard library validate everything as you write.