
Understand it
Start with soundness, data loss, and observable behavior before naming cosmetic issues. Explain impact, point to evidence, and offer a smaller safer change instead of rewriting to personal taste.
A review should help the author ship correct, maintainable code. Begin with user-visible behavior, data integrity, security assumptions, and concurrency or memory safety. Then examine API contracts and tests. Style matters when it obscures meaning or violates team conventions, but a naming preference is less urgent than a lost error result. Point to a concrete line, explain the failure scenario, and suggest a smaller change that addresses it. Separate a blocking issue from a suggestion.
For Rust code, watch for unwrap() on external input, cloned data hiding ownership confusion, a lock held across slow work, and unsafe code whose safety comment does not prove its preconditions. Also look for tests that cover only the happy path while a boundary case can break the function. Do not label every unwrap() a bug; a proven invariant in a test or startup configuration can justify it. Ask what input makes the line fail.
In an interview, review one issue at a time. Say what can happen, how likely it is given the inputs, and how you would verify a fix. Offer a test case when possible. If there is a larger refactor, explain whether it is necessary for the current change or can follow later. A good review is evidence and prioritization delivered respectfully, not an opportunity to rewrite another engineer's style.
Interview follow-up: When would Result be a better contract than Option? Add tests for empty input and a Unicode first string.
Read the code
fn first(values: &[String]) -> &str {
values[0].as_str()
}values[0] panics on an empty slice, so the signature promises a &str the body cannot always deliver.
Try it
Review first as if in a pull request: name the input that breaks it, the impact, and the smallest change that fixes it.
What success looks like: Your finding points at values[0], states the empty-input panic, and offers Option<&str> before any style comment.
Check your understanding
Which API change handles empty input without returning a fabricated value?
Reveal the answer
- Return Option<&str> using values.first().map(String::as_str).
- Clone values[0] before indexing to remove the empty-input failure.
- Mark the function unsafe so indexing no longer checks its bounds.
The returned borrow can remain tied to the input. Option expresses absence; cloning does not fix the invalid index.
Go deeper
Common misconception: Clone values[0] before indexing to remove the empty-input failure.
values[0] still panics on an empty slice, because the clone needs that element first. A clone cannot supply an element that is not there.
- Identify the contract: Prioritize correctness, API contracts, then style.
- Trace the concrete example:
values[0]panics on an empty slice, so the signature promises a&strthe body cannot always deliver. - Test the changed constraint: When would Result be a better contract than Option? Add tests for empty input and a Unicode first string.
Transfer challenge: When would Result be a better contract than Option? Add tests for empty input and a Unicode first string.
Connections
- Clarify Requirements: Turn a broad prompt into boundaries and invariants
- Explain an Incident: Describe conditions and controls, not one guilty person
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Review Rust Code (courseId "interview", lessonId "q-code-review") Link: https://crabrix.com/learn/interview/q-code-review/ Goal: Start with soundness, data loss, and observable behavior before naming cosmetic issues. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-code-review) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.