Skip to lesson content
navan✳THE BUILD LABRUST
0/53 complete
BUILD LAB / RUST/ CONFIDENCE IN THE BUILD
BUILD NOTE 37 / 53

Use the feedback loop.

Read the first error, reduce the example, and verify the fix.

BY NAVAN SAMI◷ 7 MIN READFOUNDATIONSRUST 2024

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.

lessons/debugging/src/main.rs
fn borrow_len(s: &str) -> usize {
    s.len()
}
fn main() {
    let name = String::from("Rust");
    let bytes = borrow_len(&name);
    println!("{name}: {bytes} bytes");
}
↳ EXPECTED OUTPUT
Rust: 4 bytes
  1. 1

    The function borrows because it only needs a read-only view.

  2. 2

    The owner remains available for printing.

  3. 3

    Try changing the parameter to String locally to inspect how a move changes the contract.

From the extracted rust-workbench folder
cargo run -p lesson_debugging

03 Mind the boundary

04 Your next build

A SMALL MISSION

Refactor a consuming length function to accept a borrowed slice and prove the vector is still usable.

Reveal one possible solution +
solutions/debugging/src/main.rs
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

QUICK SIGNAL CHECK

What is the best first response to a borrow-checker error?

ONE MORE CONTRACT YOU CAN EXPLAIN.

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.

Built with curiosity. By Navan Sami. ↗Rust · Make the contract clear. Then build.