【发布时间】:2015-10-23 04:32:35
【问题描述】:
我有一些由管道| 符号分隔的文件内容。命名为important.txt。
1|130|80|120|110|E
2|290|420|90|70|B
3|100|220|30|80|C
然后,我使用 Rust BufReader::split 来读取它的内容。
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::Prelude::*;
use std::path::Path;
fn main() {
let path = Path::new("important.txt");
let display = path.display();
//Open read-only
let file = match File::open(&path) {
Err(why) => panic!("can't open {}: {}", display,
Error::description(why)),
Ok(file) => file,
}
//Read each line
let reader = BufReader::new(&file);
for vars in reader.split(b'|') {
println!("{:?}\n", vars.unwrap());
}
}
问题是,vars.unwrap() 会返回字节而不是字符串。
[49]
[49, 51, 48]
[56, 48]
[49, 50, 48]
[49, 49, 48]
[69, 10, 50]
[50, 57, 48]
[52, 50, 48]
[57, 48]
[55, 48]
[66, 10, 51]
[49, 48, 48]
[50, 50, 48]
[51, 48]
[56, 48]
[67, 10]
你知道如何在 Rust 中将这个分隔文件解析为变量吗?
【问题讨论】:
标签: rust