0Pricing
Learn Rust Coding · Lesson

String vs &str

Ownership of text.

String vs &str is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Owned vs Borrowed Text

Rust separates text ownership into two types:

  • Stringowns its data on the heap.
  • &strborrows a view of text it does not own.
fn main() {
    let owned = String::from("I own this");
    let borrowed: &str = "I am borrowed";
    println!("{} / {}", owned, borrowed);
}

Where the Data Lives

A String stores its bytes on the heap and can grow. A string literal &str is baked into the program binary and is fixed.

fn main() {
    let mut heap = String::from("grows");
    heap.push_str(" bigger");
    let fixed = "never changes";
    println!("{} | {}", heap, fixed);
}

Ownership Means Responsibility

The owner of a String frees its memory when it goes out of scope. A &str owns nothing, so it never frees anything.

fn main() {
    {
        let s = String::from("temporary");
        println!("{}", s);
    } // s is dropped and its memory freed here
    println!("done");
}

Borrowing a String

You can borrow a String as a &str using &. The String still owns the data; the slice just looks at it.

fn main() {
    let owned = String::from("shared text");
    let view: &str = &owned;
    println!("{} / {}", owned, view);
}

Moving a String

Assigning a String to another variable moves ownership. The original can no longer be used.

fn main() {
    let a = String::from("hello");
    let b = a; // ownership moves to b
    // println!("{}", a); // would error: a was moved
    println!("{}", b);
}

Cloning to Keep Both

If you need two independent owned strings, use clone to copy the data.

fn main() {
    let a = String::from("hello");
    let b = a.clone();
    println!("{} and {}", a, b);
}

Copying a &str Is Cheap

A &str is just a pointer and length, so copying it does not copy the underlying text. It implements Copy.

fn main() {
    let original = "data";
    let copy = original; // both still valid
    println!("{} {}", original, copy);
}

Functions and Ownership

Passing a String by value moves it into the function. Passing &str or &String only borrows, leaving the caller's value usable.

fn length(s: &str) -> usize {
    s.len()
}

fn main() {
    let text = String::from("measure me");
    println!("{}", length(&text));
    println!("still here: {}", text);
}

Returning Owned Strings

When a function must produce new text, it returns an owned String so the caller takes ownership.

fn make_greeting(name: &str) -> String {
    format!("Hello, {}!", name)
}

fn main() {
    let g = make_greeting("Rust");
    println!("{}", g);
}

Choosing the Right Type

Guidelines:

  • Store owned text? Use String.
  • Just read or pass text? Accept &str.
  • Build new text in a function? Return String.
fn first_word(s: &str) -> &str {
    s.split_whitespace().next().unwrap_or("")
}

fn main() {
    let sentence = String::from("learn rust today");
    println!("{}", first_word(&sentence));
}

Why the Distinction Matters

This split lets Rust avoid unnecessary copies and guarantees memory safety without a garbage collector. You always know who owns a piece of text.

fn main() {
    let owned = String::from("safe");
    let borrowed = &owned[..];
    println!("owner {} borrows {}", owned, borrowed);
}

Quick Check

Recall what happens when you move a String.

Recap

String vs &str ownership:

  • String owns heap data and frees it on drop.
  • &str borrows existing text and owns nothing.
  • Moving a String invalidates the original; clone copies it.
  • Accept &str to read, return String to produce new text.
fn shout(s: &str) -> String {
    s.to_uppercase()
}

fn main() {
    let name = String::from("rust");
    let loud = shout(&name);
    println!("{} -> {}", name, loud);
}

Frequently asked questions

Is the “String vs &str” lesson free?

Yes — the full text of “String vs &str” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “String vs &str”?

Ownership of text. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “String vs &str” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. String vs &str
  2. Slices
  3. String Methods
  4. UTF-8 and Chars
← Back to Learn Rust Coding