R Rust by Evidence PDF

Compiler Field Guide

Rust errors are usually relationship errors. Start with the question matching the diagnostic.

Diagnostic clue Ask this first Common smallest fix
use of moved value Who owns the value now? Borrow it, or clone only if two owners are required.
cannot borrow ... as mutable Is the binding mutable, and is another borrow active? Add mut, or shorten the other borrow's last use.
cannot borrow ... more than once Can two writers reach the same value? Finish the first mutable borrow before creating the next.
does not live long enough What value would the reference outlive? Return owned data or move the owner outward.
missing lifetime specifier Which input does the returned reference come from? Express that relationship with a lifetime parameter.
mismatched types What exact type does this expression produce? Convert at the boundary; do not cast blindly.
trait bound ... is not satisfied Which operation requires which capability? Add the narrow bound, or stop asking the generic code to do that operation.
non-exhaustive match Which valid state is untreated? Handle the missing variant explicitly.
future cannot be sent safely What non-Send value crosses an .await? Drop it before .await, or use an appropriate thread-safe type.

A useful reading order

  1. Read the short error headline.
  2. Find the source line marked by the primary span.
  3. Read the first note; it often explains the relationship.
  4. Apply the smallest fix that matches your intent.
  5. Re-run cargo check. Later diagnostics may be consequences of the first.

Do not respond to ownership errors with automatic .clone(). That makes the compiler quiet by changing cost and ownership. Decide whether the code needs another owner first.