如何在不复制的情况下在 Rust 中迭代 Vec 时分配切片?

How To Assign Slices While Iterating Over a Vec in Rust without copying?

我正在尝试高效地逐行解析 CSV 文件,而无需分配不必要的内存。

由于我们无法在 Rust 中对字符串进行索引,我的想法是为每一行创建一个结构,它拥有一个 Vec<char> 行字符和几个 &[char] 代表位置的切片在 Vec 需要进一步处理的字段中。

我只支持英文,所以不需要 Unicode 字符。

我从 BufReader 中抓取每一行,将其收集到我的 Vec<char> 中,然后遍历字符以注意每个字段切片的正确偏移量:

let mut r_line: String;
let mut char_count: usize;
let mut comma_count: usize;
let mut payload_start: usize;
for stored in &ms7_files {
    let reader = BufReader::new(File::open(&stored.as_path()).unwrap());
    for line in reader.lines() {
        r_line = line.unwrap().to_string();
        let r_chars: Vec<char> = r_line.chars().collect();
        char_count = 0;
        comma_count = 0;
        payload_start = 0;
        for chara in r_chars {
            char_count += 1;
            if chara == ',' {
                comma_count += 1;
                if comma_count == 1 {
                    let r_itemid = &r_chars[0..char_count - 1];
                    payload_start = char_count + 1;
                } else if comma_count == 2 {
                    let r_date = &r_chars[payload_start..char_count - 1];
                    let r_payload = & r_chars[payload_start..r_line.len() - 1];
                }
            }
        }
        // Code omitted here to initialize a struct described in my
        // text above and add it to a Vec for later processing
    }
}

一切顺利,直到 if 中的代码在 comma_count 上进行测试,我尝试在 Vec 中创建字符切片。当我尝试编译时,我得到了可怕的结果:

proc_sales.rs:87:23: 87:30 error: use of moved value: `r_chars` [E0382]
proc_sales.rs:87                        let r_itemid = &r_chars[0..char_count - 1];
                                                        ^~~~~~
proc_sales.rs:87:23: 87:30 help: run `rustc --explain E0382` to see a detailed explanation
proc_sales.rs:82:17: 82:24 note: `r_chars` moved here because it has type `collections::vec::Vec<char>`, which is non-copyable
proc_sales.rs:82            for chara in r_chars {
                                     ^~~~~~~

每次尝试创建切片。我基本上可以理解为什么编译器会抱怨。我想弄清楚的是一种更好的策略来收集和处理这些数据,而无需诉诸大量复制和克隆。哎呀,如果我可以保留 BufReader 所拥有的原始 String(对于每个文件行),并且只保留其中的切片,我会的!

欢迎就修复上述代码发表评论,并就此问题的替代方法提出建议以限制不必要的复制。

BufReader::lines returns 一个生成 Result<String> 项的迭代器。当在这样的项目上调用 unwrap 时,它总是会分配一个新的 String(请注意,在 line.unwrap().to_string() 中,to_string() 是多余的)。

如果你想最小化分配,你可以使用BufReader::read_line

要拆分 CSV 行的字段,您可以使用 str::split

这是您的代码的简化版本:

use std::io::{BufRead, BufReader};
use std::fs::File;

fn main() {
    let mut line = String::new();
    let ms7_files = ["file1.cvs", "file2.cvs"];
    for stored in &ms7_files {
        let mut reader = BufReader::new(File::open(stored).unwrap());
        while reader.read_line(&mut line).unwrap() > 0 {
            // creates a scope to the iterator, so we can call line.clear()
            {
                // does not allocate
                let mut it = line.split(',');
                // item_id, date and payload are string slices, that is &str
                let item_id = it.next().expect("no item_id fied");
                let date = it.next().expect("no date field");
                let payload = it.next().expect("no payload field");
                // process fields
            }
            // sets len of line to 0, but does not deallocate
            line.clear()
        }
    }
}

您可能还想查看 various crates to work with CSV files。

对于你的问题,正如@Simon Whitehead 回答的那样,r_chars 的所有权已经转移到构建 IntoIterator,因此你不能使用它。

使用

修改您的代码
for chara in &r_chars
// equivalent to
// for chara in r_chars.iter()

并且随时复制 *chara(便宜)可能会修复它。

对于@malbarbo 的回答,如果您的 csv 包含文本列(它本身可以包含换行符),我建议不要使用 BufReader::lines

正在查看 crates.io I instead advise using either the battle tested csv or if you need slightly more performance but are ready for much less tested one quick-csv