Use the feedback loop.
Read the first error, reduce the example, and verify the fix.
01 Understand the contract
Compiler diagnostics show a primary error, relevant source locations, and often a suggested explanation. Read the ownership or type relationship before changing syntax at random. cargo check gives quick type feedback; rustc --explain E0382 explains a moved-value error. cargo fmt standardizes layout and cargo clippy provides additional lints. Debug output and dbg! help inspect a value during development, but logs should be intentional before release. A small reproducible example often reveals the actual contract problem faster than a large rewrite.
02 Make it concrete
This is a complete standard-library example. Use Rust stable with the 2024 edition. Run it locally using the Rust workbench.
fn borrow_len(s: &str) -> usize {
s.len()
}
fn main() {
let name = String::from("Rust");
let bytes = borrow_len(&name);
println!("{name}: {bytes} bytes");
}Rust: 4 bytes
- 1
The function borrows because it only needs a read-only view.
- 2
The owner remains available for printing.
- 3
Try changing the parameter to String locally to inspect how a move changes the contract.
cargo run -p lesson_debugging03 Mind the boundary
04 Your next build
Refactor a consuming length function to accept a borrowed slice and prove the vector is still usable.
Reveal one possible solution +
fn count<T>(values: &[T]) -> usize {
values.len()
}
fn main() {
let v = vec![1, 2, 3];
assert_eq!(count(&v), 3);
assert_eq!(v[0], 1);
}05 Check your reasoning
What is the best first response to a borrow-checker error?
Understand it.
Then make it yours.
Run the example, try your own input, and explain the boundary. Log the lesson when the idea makes sense.