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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A few boolean flags turn a character iterator into a tiny state machine that parses without regex or backtracking.
- 2std::mem::take swaps a value out and leaves a default behind, avoiding a clone when you want to keep reusing the buffer.
- 3Handling the escape case before the main match keeps escaped characters from being interpreted as syntax.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/parsing-quoted-fields-with-a-state-machine-in-rust-explained-rust-639f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.