CSV Cleaner: First Rust Project

Hi. I'm new to Rust. I've written up a little opensource tool to clean CSV files as a practical learning exercise that will help me with my job: GitHub - ambidextrous/csv_cleaner: CLI tool for row-wise cleaning of CSV files with logging of destructive operations

The script is a literally a result of my having read half of the Rust book and then battled the borrow checker until the thing compiled. I don't know any Rust programmers personally, so I'd really appreciate it if someone could take a look at the code and tell me what I'm doing wrong.

For context, I'm a data engineer who works mainly with Python and SQL, and am looking to learn some Rust to speed up the bottlenecks in my code. I have zero experience in systems programming languages.

2 Likes

Honestly it looks pretty good. I'm used to scanning first efforts being far more painful :wink: Nice to see you're using the csv crate.

One thing that can save you some effort is if let, especially for Options.

For example, I saw a couple of things like

match &column_log.max_invalid {
    Some(x) => {
        max_val = x.clone();
    }
    None => {}
}

And you can make that a bunch shorter by doing

if let Some(x) = &column_log.max_invalid {
    max_val = x.clone();
}

(Basically if you're writing None => {} that's a good hint to take a look at whether there's another way you can do that.)

As an entirely "your milage may vary", thing, I saw

let empty_vec: Vec<String> = Vec::new();
let empty_string = "".to_string();
let mut column_map = HashMap::new();

You can also write String::new() to get an empty string, if you prefer that.

"".to_string(), String::new(), "".to_owned(), String::default(), etc are all fine, so it's just a style choice. But if you like Vec::new() over vec![], then I thought you might like String::new() in that snippit too.

8 Likes

I only gave it a brief look over, but one thing that you may want to know about is the crate anyhow. Since it seems that for the most part you are just bubbling these errors up to main and printing it out. Keep up the good work :slight_smile:

2 Likes

Much appreciated. Thanks.

Thanks for the tip. Error handling in Rust is one of the things that I'm having trouble getting my head around, and after a look through the anyhow docs, what it does still isn't entirely clear to me. What would be the advantage of using anyhow over the way the error handling is currently being handled in the code?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.