liveBook · Manning

Ownership

As explained in Learn Rust in a Month of Lunches

Ownership is Rust's rule-based model in which each value has a responsible binding that controls the value's lifetime and access.

Ownership answers a simple question: who is responsible for a value? In Rust, a value has one owner. A reference lets someone else use that value without taking ownership. Think of lending a coworker a view of your computer screen: they can look, but the computer remains yours. section 2.7 section 2.10

The difference matters when you pass a String to a function. A parameter of type String takes ownership. Passing the String is called a move: the function receives the same data, rather than making another allocation. Afterward, the original variable cannot be used. If the function does not return the String, that value is cleaned up when the function finishes. section 2.7

A second call after a move

Suppose print_country takes a String. After print_country(country), a second call with country produces a “use of moved value” error. The compiler points to both the first call, where ownership moved, and the attempted reuse. It is telling you where to change how the function receives the value. section 2.7

If a function only needs to read the String, give it a reference instead: change its parameter to &String and call it with &country. The function borrows the data, so you can call it again with the same owner. If it needs to change the String, use a mutable String and pass &mut country to a parameter of type &mut String. Shared reading is fine; two borrowers should not both be changing the same value at once. section 2.7 section 2.10

A reference must not outlive the value it points to. A function cannot create a local String and return a reference to it: the String goes away when the function ends. The compiler rejects that return rather than leaving a reference to memory that has been cleaned up. Returning an owned String is different because ownership can travel with it. section 2.4 section 2.7

Not every argument moves. Simple Copy types, including integers, are copied automatically when passed to a function, so the original can be used again. String is not Copy. You can explicitly call .clone() when you need another String, but borrowing avoids making that extra copy when the function only needs to inspect it. This is why a function called repeatedly to count words can take &String instead of cloning the growing String each time. section 2.8

A useful first check is: does this operation need to take the value, merely read it, or change it temporarily? Choose an owned value, a shared reference, or a mutable reference accordingly. The computer analogy is not perfect, but it helps keep the central rule in view: lending access is not the same as giving ownership away. section 2.10 section 2.7

How Learn Rust in a Month of Lunches covers it

How deep it goes
Works through moves, borrowing fixes, Copy types, and compiler errors for moved values and invalid returned references.
Examples
Contrasts a String moved into repeated function calls with borrowed Strings, copied integers, and a rejected reference to a local String.
What it assumes
Builds on variables and function calls while introducing references as borrowing and explaining moves through examples.
What you can do after
Diagnose/debug with it when a function call moves a String or a returned reference points to a local value.
How it connects
Connects references, mutable borrowing, String, Copy types, cloning, and value cleanup.

Also explained in

Compare how 6 books explain ownership →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.