rust 33 lines · 7 steps

Parsing quoted fields with a state machine in Rust

A single-pass scanner that walks a string character by character, tracking quote and escape state to pull out quoted substrings.

Explained by highlit
1pub fn extract_quoted_fields(line: &str) -> Vec<String> {
2 let mut fields = Vec::new();
3 let mut current = String::new();
4 let mut in_quotes = false;
5 let mut escaped = false;
6 let mut chars = line.chars();
7 
8 while let Some(c) = chars.next() {
9 if escaped {
10 match c {
11 'n' => current.push('\n'),
12 't' => current.push('\t'),
13 'r' => current.push('\r'),
14 other => current.push(other),
15 }
16 escaped = false;
17 continue;
18 }
19 
20 match c {
21 '\\' if in_quotes => escaped = true,
22 '"' if in_quotes => {
23 fields.push(std::mem::take(&mut current));
24 in_quotes = false;
25 }
26 '"' => in_quotes = true,
27 _ if in_quotes => current.push(c),
28 _ => {}
29 }
30 }
31 
32 fields
33}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A few boolean flags turn a character iterator into a tiny state machine that parses without regex or backtracking.
  2. 2std::mem::take swaps a value out and leaves a default behind, avoiding a clone when you want to keep reusing the buffer.
  3. 3Handling the escape case before the main match keeps escaped characters from being interpreted as syntax.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Parsing quoted fields with a state machine in Rust — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code