Diagnostics and static analysis
Quick answer
Compiler diagnostics describe problems detected during compilation. Static analysis examines source or intermediate representations without requiring a normal application run.
Separate errors from lints
Start from the primary span, read the explanation and related spans, then make the smallest correction. rustc rejects invalid programs; Clippy adds lint-based guidance, which can include stylistic or suspicious patterns. Cargo check does not perform the full final code generation and linking of a build. Automated suggestions need human review, and passing static checks does not prove business correctness.
fn main() { let message = String::from("hi"); let _owned = message; println!("{message}");}Expected: compilation fails; exact diagnostic not verified.
Follow the moved value rather than blindly adding clone.
# In a trusted scratch Cargo project, with Clippy installed:cargo +1.85.0 check --all-targetscargo +1.85.0 clippy --all-targets -- -D warningsrustc +1.85.0 --explain E0382Manual terminal step; not executed. Expected behavior (environment dependent):
Checks and explanation; success depends on source and installed components.The warning policy is explicit and may need adjustments for a real project.
Common mistake
Do not suppress every warning globally; distinguish a deliberate exception from an actual defect.
Try it yourself
Fix the move example by borrowing instead of moving, then explain why the owner remains valid.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Diagnostics and static analysis — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Clippy and cargo check succeed. Which important evidence is still missing?
Diagnostics and static analysis — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let message = String::from("hi"); let _owned = message; println!("{message}"); } ``` Which outcome or interpretation best matches this example?
Diagnostics and static analysis — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```bash # In a trusted scratch Cargo project, with Clippy installed: cargo +1.85.0 check --all-targets cargo +1.85.0 clippy --all-targets -- -D warnings rustc +1.85.0 --explain E0382 ``` Which outcome or interpretation best matches this example?