
Understand it
A strong answer defines input ownership, source spans, recovery strategy, and whether the parser returns a tree or events. Separate lexing from grammar only when that boundary improves errors or complexity.
A parser design starts with the language and its error contract. Is input trusted? Must the parser preserve exact source positions? Should it return one tree, a stream of events, or partial results with diagnostics? Define a small grammar and one ambiguous case before choosing an algorithm. A tokenizer can simplify grammar code by turning characters into identifiers, numbers, and punctuation with spans. For a tiny format, a separate lexer may be unnecessary; choose the boundary that improves clarity and error messages.
For a configuration file, make the parser distinguish a missing field, an invalid number, and an unexpected token. Each diagnostic should point to a source span and explain what was expected. If recovery is required, decide where to resume after an error so one bad line does not hide every later issue. Avoid panic on malformed input. Parsing is the boundary from bytes or text into validated domain values, so a successful result should satisfy invariants the rest of the program can trust.
In an interview, show one input moving through tokenization, grammar recognition, AST construction, and validation. Discuss ownership of source text: copying every token into a String may be simple but expensive, while borrowing slices ties results to the input lifetime. Test empty input, nested structures, invalid UTF-8 if bytes are accepted, and a deeply nested case that could exhaust recursion. The answer should explain how errors help a user fix a file, not just how the happy path builds a tree.
Interview follow-up: How do byte offsets, Unicode scalar positions, grapheme clusters and editor UTF-16 selections differ?
Read the code
struct Span { start: usize, end: usize }
fn main() {
let source = "café";
let span = Span { start: 3, end: 5 };
assert_eq!(source.get(span.start..span.end), Some("é"));
}Spans are coordinates, not ownership or proof of validity. Keep the correct source/version and use checked slicing at trust boundaries.
Try it
Trace Span { start: 3, end: 5 } over café, then say which ranges get rejects and who must keep the source alive.
What success looks like: You state the UTF-8 boundary check and explain why a Span owns nothing.
Check your understanding
What must a byte span into a UTF-8 source preserve before it becomes a &str slice?
Reveal the answer
- The range must be ordered, in bounds and aligned to UTF-8 character boundaries.
- Every byte offset is automatically a valid Unicode scalar boundary.
- Storing a Span automatically extends the lifetime of its source buffer.
Spans are coordinates, not ownership or proof of validity. Keep the correct source/version and use checked slicing at trust boundaries.
Go deeper
Common misconception: Every byte offset is automatically a valid Unicode scalar boundary.
Only some offsets are boundaries. In café, byte 4 falls inside the two-byte é, so get(3..4) returns None.
- Identify the contract: Separate tokenization, grammar, and diagnostics.
- Trace the concrete example: Bytes 3 to 5 are exactly
é, sogetreturns it. A range that splits those two bytes returnsNone. - Test the changed constraint: How do byte offsets, Unicode scalar positions, grapheme clusters and editor UTF-16 selections differ?
Transfer challenge: How do byte offsets, Unicode scalar positions, grapheme clusters and editor UTF-16 selections differ?
Connections
- Design an LRU Cache: Combine lookup speed with recency updates
- Debugging Out Loud: Form a hypothesis and request one discriminating fact
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: Design a Parser (courseId "interview", lessonId "q-parser-design") Link: https://crabrix.com/learn/interview/q-parser-design/ Goal: A strong answer defines input ownership, source spans, recovery strategy, and whether the parser returns a tree or events. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-parser-design) 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.