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 std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::thread; use std::time::Duration;
Graceful thread shutdown with an atomic flag
concurrency
atomics
memory-ordering
Advanced
7 steps
rust
use std::convert::TryFrom; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum HttpStatus {
Converting HTTP codes with TryFrom in Rust
enums
error-handling
trait-implementation
Intermediate
8 steps
rust
use axum::{ extract::{Request, State}, http::{HeaderValue, StatusCode}, middleware::Next,
API version headers with Axum middleware
middleware
http-headers
versioning
Intermediate
8 steps
rust
use axum::{ extract::Path, http::StatusCode, routing::{get, post},
Building a REST resource in Axum
rest-api
routing
serialization
Intermediate
9 steps
rust
use chrono::NaiveDate; use serde::{Deserialize, Deserializer}; #[derive(Debug, Deserialize)]
Custom date parsing with serde in Rust
serde
deserialization
csv-parsing
Intermediate
7 steps
rust
use axum::{extract::State, http::StatusCode, Json}; use serde::{Deserialize, Serialize}; use sqlx::PgPool; use uuid::Uuid;
An atomic money transfer handler in Axum
database-transactions
atomicity
error-handling
Intermediate
9 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.