Ownership in Practice · 16 min · free lesson

Owned or Borrowed?

Choose a return type that matches the data flow.

Core idea

Return a borrow when the result is a view into an input; return an owned value when new data is created or must escape independently.

A trimmed view points into the original text while uppercase transformation produces a new owned buffer.
Borrow for a view into existing bytes; own newly created bytes.

Understand it

Return a borrow when the result is a view into an input; return an owned value when new data is created or must escape independently. The choice affects allocation and API flexibility.

The lifetime of the data behind a return value should determine its type. trimmed(input: &str) -> &str returns a view into existing bytes, so it can avoid allocation, but the caller cannot keep the result after the input is gone. label(input: &str) -> String creates uppercase text in a new owned buffer. The caller may keep that buffer independently of the input. Neither choice is universally better; each promises a different relationship.

Follow the example with a String created in an inner block. A borrowed trimmed view must be used before the source leaves the block. An owned uppercase result can be moved out and printed afterward. If a function sometimes borrows and sometimes must allocate, std::borrow::Cow<'a, str> can express either case, but it adds complexity that a beginner API may not need. First choose a clear owned or borrowed contract, then optimize if measurements justify it.

Do not return a reference to a local String assembled inside a function; the value would be dropped when the function returns. Return that String by value instead, relying on move semantics. Conversely, do not allocate just to return a substring that the caller will use briefly while the input remains alive. Test both signatures with a string literal and with an owned string. Ask who owns the bytes after the call and how long the result must remain valid; those answers should make the return type feel inevitable.

A borrowed return can still be ergonomic: callers can call .to_owned() only when they need independence. That lets the common short-lived path avoid allocation while preserving an explicit escape hatch.

Read the code

fn label(input: &str) -> String {
    input.trim().to_uppercase()
}
fn trimmed(input: &str) -> &str {
    input.trim()
}

The example makes the contract visible in code.

Practise this code in Playground ↗

Try it

Decide for label and trimmed who owns the returned bytes.

What success looks like: You can say which result can outlive input, and why each return type fits its body.

Check your understanding

Why does label return String?

Reveal the answer
  1. It creates new text
  2. &str is slower
  3. trim requires it

Correct: It creates new text.

Go deeper

Common misconception: “label returns String because &str is slower.”

Speed is not the reason. to_uppercase() builds new text, so there is nothing in input to borrow. trimmed only narrows existing bytes, so it can return &str.

  1. Ask where the bytes live: trimmed returns a slice of the bytes in input. label calls to_uppercase(), which builds a new String the caller will own.
  2. Predict before checking: Call both on a String made in an inner block and assign the results to outer variables. Predict which one can be printed after the block.
  3. Compare with evidence: The result of label prints after the block. The result of trimmed gets E0597, because its source is gone; it has to be used inside the block.

Transfer challenge: Add .to_owned() to the trimmed call so its result escapes too. Then write fn shout(input: &str) -> &str that returns the uppercase text and read why the compiler refuses to return a reference to a local value.

Connections

  • Lifetime Elision: The common lifetime relationships Rust infers
  • Trait Objects: Choose behavior at runtime with dyn Trait
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Owned or Borrowed? (courseId "ownership", lessonId "owned-or-borrowed")
Link: https://crabrix.com/learn/ownership/owned-or-borrowed/
Goal: Return a borrow when the result is a view into an input; return an owned value when new data is created or must escape independently.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/owned-or-borrowed) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.