Decide who manages a value
Ownership describes how responsibility for using and eventually releasing a value is associated with program entities. We use Rust String to distinguish an owning binding from a reference that borrows access to its value. This is not document ownership or user authorization; it concerns lifetime-related type rules. Moving ownership of a String into another binding prevents equivalent subsequent use through the old binding. Borrowing instead permits access without transferring ownership under the applicable conditions. These distinctions help explain whether a caller can use a value afterward and when a reference remains valid. Not every Rust assignment behaves as this String example does: types implementing Copy have different behavior. Keep the non-Copy premise explicit instead of turning one teaching example into a universal rule about assignment.
Trace borrowing and mutation in time
In the successful program, text owns the String cat and view borrows a shared reference to it. The program checks its length and prints cat, then appends s through text. The last use of view precedes that mutation, so this example has no remaining conflicting use of the shared borrow at the mutation point. A reference name appearing inside the same braces does not alone establish a borrow lasting to the final brace. Ownership then moves from text to moved, which prints cats. The lengths three and four are byte counts for these ASCII strings, not a universal measure of displayed Unicode characters. Draw ownership and reference-use intervals before execution. The contract concerns which bindings may use the value; printing physical addresses is unnecessary to explain it.
fn main() {
let mut text = String::from("cat");
let view = &text;
assert_eq!(view.len(), 3);
println!("{}", view);
text.push('s');
let moved = text;
assert_eq!(moved.len(), 4);
println!("{}", moved);
}Verify success and failure under one configuration
The recorded environment was Linux with rustc 1.93.1 and edition 2021. Save the successful example as borrow_ok.rs, compile using rustc --edition=2021 borrow_ok.rs, then execute the resulting binary. It prints cat and cats on separate lines and passes both length assertions. Save each failing example in a separate file and compile under the same version and edition. Combining unrelated failures can make it unclear which intended condition was actually checked. Execution evidence retains source hashes, exit codes and diagnostic identifiers. Exact wording and line presentation may change in another compiler version, so connect a rejected use to the preceding move or borrow rather than comparing formatting alone. Bypassing type safety solely to silence a diagnostic would not fulfill this experiment’s objective.
| Order | Operation | Explanation |
|---|---|---|
| 1 | Create String | text owns it |
| 2 | Bind &text to view | Borrow a shared reference |
| 3 | Use view for the last time | Final use requiring this borrow in the example |
| 4 | text.push | No later use of the previous shared reference |
| 5 | moved = text | Move ownership of the non-Copy value |
A moved non-Copy binding cannot be reused
The move failure transfers text into moved and then tries to print both bindings. Because String is not Copy here, the later use of text is rejected; the recorded compiler reports E0382. This can be understood as preventing arbitrary creation of two independent ownership responsibilities for the same value. It does not specify exactly how every byte moves at a physical address in optimized machine code. Ownership semantics and machine implementation are different layers. If an independent value is genuinely required, an explicit clone is one option, but adding clones indiscriminately conceals costs and intent. Choose a reference for temporary access and a move for ownership transfer according to the responsibility needed by the operation.
fn main() {
let text = String::from("cat");
let moved = text;
println!("{}", moved);
println!("{}", text);
}Reject mutation that conflicts with later reference use
The second failing example creates view, mutates text and then prints view. Unlike the successful program, the shared reference is used after the mutation. The mutable access needed by push conflicts with that continuing shared use, and the recorded compiler reports E0502. The presence of mut on text does not override borrowing rules at the mutation point. Potential repairs include finishing the reference use earlier, borrowing after the update, or deliberately creating an independent value when that is the actual requirement. These repairs are not interchangeable. Decide whether the later consumer needs the old content or the updated content before changing program order simply to obtain successful compilation.
fn main() {
let mut text = String::from("cat");
let view = &text;
text.push('s');
println!("{}", view);
}Lifetime annotations describe relationships, not extension commands
A valid reference requires its target value to remain available for the needed period and its access rules to hold. Lifetime annotations describe relationships among reference lifetimes in interfaces; writing an annotation does not command a local value to survive after it would otherwise cease to exist. If a function attempts to return a reference into a local String that will no longer be available, consider returning an owned value or another appropriate ownership design instead. Do not reduce the rules to counting physical addresses. Multiple shared references and the conditions for mutable references are defined by the type system’s contracts. These short examples introduce that reasoning without claiming to cover every lifetime inference or advanced type mechanism.
Keep memory rules distinct from application correctness
These examples reject the illustrated inconsistencies in moves and borrows. They do not establish that an application computes the intended business result, authorizes document updates correctly or handles network failure. Safe Rust can still retain unneeded data or implement an incorrect business decision. FFI and unsafe boundaries introduce conditions that must be met before relying on ordinary typed interfaces. A particular rejected borrow is also not proof that every concurrency problem has been solved. Synchronization, state transitions, cancellation and completion retain separate contracts. State which errors a mechanism helps detect early and which responsibilities remain. That is more useful than treating ownership as a universal label for every desirable form of safety.
Choose copying from the required independence
Keeping both an original value and a destination does not imply the same copying operation for every type. Copy types such as integers differ from the String in this example. Choosing String::clone means asking whether independently owned string contents are required and whether their allocation and copying cost is acceptable. Adding clone merely to silence an error omits the reason two owners were needed. Distinguish a borrowed view from an independent value, then choose the operation that expresses the requirement rather than treating duplication as a universal ownership repair.
Exercise: identify ownership and last use
Move the final println in the conflicting-borrow example before text.push. The reference’s last use now precedes mutation, matching the successful program’s relationship. In the move example, removing the final use of text leaves a use of moved consistent with ownership transfer. If the original binding must still supply data later, reconsider whether the requirement calls for a move, borrow or deliberate clone. Finally replace cat with Japanese text and ask whether len should be called a displayed character count. It should not: this String length is measured in UTF-8 bytes. State units as well as ownership so the example does not introduce an unrelated misconception. After each edit, return to a diagram of the owner and reference-use intervals instead of relying only on whether the compiler stopped reporting an error.
Keep a buffer alive while a virtual device can access it
Returning from a software function is different from completion of an external device request. After publication through a virtqueue, the request owner must retain the referenced buffers until completion has been matched to that request. Reusing request A’s output region as B’s input could allow a late result for A to corrupt B. Rust can express software ownership, but it does not automatically observe device completion. At unsafe and FFI boundaries, access duration, address stability, and responsibility for reclamation are additional contracts. Draw the function lifetime and the in-flight request lifetime separately before beginning the virtualization exercises.