rust
54 lines · 9 steps
Parsing a CSV line with a state machine
A four-state finite state machine walks a line character by character to split fields while respecting quotes and escaped quotes.
Explained by
highlit
1#[derive(Debug, PartialEq)]
2enum State {
3 FieldStart,
4 InUnquoted,
5 InQuoted,
6 QuoteInQuoted,
7}
8
9pub fn parse_csv_line(line: &str) -> Vec<String> {
10 let mut fields = Vec::new();
11 let mut current = String::new();
12 let mut state = State::FieldStart;
13
14 for ch in line.chars() {
15 match state {
16 State::FieldStart => match ch {
17 '"' => state = State::InQuoted,
18 ',' => fields.push(std::mem::take(&mut current)),
19 _ => {
20 current.push(ch);
21 state = State::InUnquoted;
22 }
23 },
24 State::InUnquoted => match ch {
25 ',' => {
26 fields.push(std::mem::take(&mut current));
27 state = State::FieldStart;
28 }
29 _ => current.push(ch),
30 },
31 State::InQuoted => match ch {
32 '"' => state = State::QuoteInQuoted,
33 _ => current.push(ch),
34 },
35 State::QuoteInQuoted => match ch {
36 '"' => {
37 current.push('"');
38 state = State::InQuoted;
39 }
40 ',' => {
41 fields.push(std::mem::take(&mut current));
42 state = State::FieldStart;
43 }
44 _ => {
45 current.push(ch);
46 state = State::InUnquoted;
47 }
48 },
49 }
50 }
51
52 fields.push(current);
53 fields
54}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Modeling parsing as explicit states makes tricky edge cases like quoted commas and escaped quotes tractable.
- 2std::mem::take swaps a value out and leaves a default behind, avoiding an extra allocation when flushing a buffer.
- 3A trailing flush after the loop handles the final field that has no delimiter to trigger it.
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-a-csv-line-with-a-state-machine-explained-rust-175a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.