Rust for a C programmer · Lesson 01 · 5–10 minutes
Follow a String through a call
Predict who owns a string after a function call, then use Cargo to check your reasoning.
Run the supplied project
Cargo builds Rust projects and manages their dependencies. Open
Cargo.toml, the package configuration, and
src/main.rs, this program’s source.
Cargo introduction.
cd "/home/dmedeiros/Documents/ChatGPT/Teach Me Rust/exercises/ownership"
cargo run
Expect report.txt on standard output. cargo run builds and runs the program;
cargo check checks compilation without producing the executable.
Rust Book: building, running, and checking.
Track responsibility for cleanup
In C, passing a pointer copies its value; your program’s conventions determine who frees the allocation.
A Rust String owns a growable UTF-8 buffer. Its owner is responsible for
the value’s cleanup. Passing this String by value moves it: ownership transfers
to the parameter, and the caller can no longer use the moved value.
Ownership and functions ·
Rust Book: the String type.
This version of show takes ownership so we can follow cleanup.
The next lesson will keep ownership with the caller.
The supplied src/main.rs
fn show(name: String) {
println!("{name}");
}
fn main() {
let name = String::from("report.txt");
show(name);
// println!("{name}");
}
fn declares a function; name: String names a parameter and its type.
let introduces a variable whose type Rust infers here. String::from creates
the owned string. println! is a formatting macro; the ! marks a macro call.
Rust Book: printing values with println! ·
Syntax reference.
Formatting can capture variable names with {name}. For a computed value,
put {} in the format string and pass the expression after the string.
Examples: printing expressions.
Predict, change, check
Before running anything else: if you uncomment the last println!, will the program compile?
Explain which variable owns the string while show is executing.
Now uncomment that line, save, and run cargo check.
Compare your prediction with the result
Compilation fails with E0382, “borrow of moved value.” The diagnostic points to the
attempted use and the earlier show(name) call. The parameter inside show
owns the string during the call.
On normal return, that parameter is dropped: Rust runs its cleanup, which releases this string’s buffer. Returning does not restore the caller’s ownership. Rust Book: ownership and cleanup in function calls.
Formatting with println! temporarily uses the string without taking ownership.
The ownership transfer happens at show(name).
Rust Book: printing and ownership.
Comment out the last line again and run cargo run to restore the working program.
One variation, from memory
Would this separate program compile? Predict its output before opening the explanation.
fn main() {
let count: u32 = 7;
let saved = count;
println!("{count} {saved}");
}
Check your reasoning
It prints 7 7. u32, an unsigned 32-bit integer, implements
Copy, Rust’s permission for implicit bitwise copying. Both variables remain usable.
String does not implement Copy. The type determines this behavior.
Rust Book: copying integer values.
Build from scratch: hand off a message
In a fresh Cargo project, write your own main and a helper named
announce. Create a String containing queued in main,
then pass ownership to announce. The helper prints the message and returns
without returning the String. After the call, main prints a separate completion message.
Expected output:
received: queued
caller finished
Use one owned String and no cloning. Run cargo check and cargo run.
Then try printing that original String from main after the call, read the
compiler diagnostic, and restore the working version.
For review: explain who owns the String during the helper call and whether its buffer
is freed before or after caller finished is printed.
The Rust Book: ownership and functions.