The CSV file format is a very common method for storing tabular data. This article covers the basics of reading and writing CSV files, (de)serializing CSV data using the Serde library, and wraps up with a sample data aggregation example.

Introduction

The idea for this article came about because I needed a fast and convenient way to work with CSV formatted data. One option was to write my own library, but I didn’t feel like that was a convenient solution. So, after a bit of research, I decided to use the csv crate authored by Andrew Gallant. I chose this crate because the API is fairly straight forward and, most importantly to me, the documentation is very thorough.

Setup

To use this crate, simply create a new Rust project using cargo new and add this under [dependencies] in the Cargo.toml file:

csv = "1.1"

Also, part of this article will utilize Serde’s custom derive functionality, so go ahead and add this as well:

serde = { version = "1", features = ["derive"] }

#rustlang #rust

Working with CSV Data in Rust
11.45 GEEK