我有以下Rust程序(rustc 1.0.0-nightly(44a287e6e 2015-01-08 17:03:40 -0800)):
use std::io::BufferedReader;
use std::io::File;
fn main() {
let path = Path::new("nc.txt");
let mut file = BufferedReader::new(File::open(&path));
let lines: Vec<String> = file.lines().map(|x| x.unwrap()).collect();
println!("{}", lines[500]);
}
Run Code Online (Sandbox Code Playgroud)
根据http://doc.rust-lang.org/std/io/上的示例,上面是将文件的行拉成字符串向量的方法.我已经投入了第500行的输出.
为了解决Python中的相同任务,我写了以下内容:
#!/usr/local/bin/python3
def main():
with open('nc.txt', 'r') as nc:
lines = nc.read().split('\n')
print("{}".format(lines[500]))
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
当我运行编译的Rust并计时时,我得到了这个:
rts@testbed $ time ./test
A declaration of independence by Kosovo will likely bring a similar declaration from Georgia's breakaway Abkhazia region, which Russia could well recognize.
./test 1.09s …Run Code Online (Sandbox Code Playgroud) rust ×1