- Rust and CSV parsing 译文(用 Rust 实现 csv 解析-part7)
- 原文链接:https://blog.burntsushi.net/csv/
- 原文作者:BurntSushi
- 译文来自:https://github.com/suhanyujie/article-transfer-rs/
- 译者:suhanyujie
- 译者博客:suhanyujie
- ps:水平有限,翻译不当之处,还请指正。
- 标签:Rust,csv
管道操作
In this section, we’re going to cover a few examples that demonstrate programs that take CSV data as input, and produce possibly transformed or filtered CSV data as output. This shows how to write a complete program that efficiently reads and writes CSV data. Rust is well positioned to perform this task, since you’ll get great performance with the convenience of a high level CSV library.
在这一节中,我们将介绍几个示例,这些示例把 CSV 数据作为输入,并对其进行转换或过滤等操作,再将结果输出。这是一个完整的有效读写 CSV 数据的程序流程。Rust 可以很好地完成这个任务,因为你可以利用高级地 CSV 库的便利性来获得出色的性能。
搜索过滤
The first example of CSV pipelining we’ll look at is a simple filter. It takes as input some CSV data on stdin and a single string query as its only positional argument, and it will produce as output CSV data that only contains rows with a field that matches the query.
我们把看到的 CSV 管道示例视为一个简单的过滤器。它把 stdin 中的一些 CSV 数据和单个字符查询作为位置参数,并且它将返回与查询相匹配的字段对应的 CSV 数据记录作为输出。
extern crate csv;
use std::env;
use std::error::Error;
use std::io;
use std::process;
fn run() -> Result<(), Box<Error>> {
// 从位置参数获取查询
// 如果没有参数,则返回错误
let query = match env::args().nth(1) {
None => return Err(From::from("e