【发布时间】:2014-12-14 22:33:49
【问题描述】:
在我第一次接触 Rust 时,我开始编写代码来将文件的内容转储到字符串中,以供以后处理(不过现在我只是将其打印出来)
有没有比我现在更清洁的方法?似乎我不得不对此过于冗长,但我没有看到任何清理它的好方法
use std::io;
use std::io::File;
use std::os;
use std::str;
fn main() {
println!("meh");
let filename = &os::args()[1];
let contents = match File::open(&Path::new(filename)).read_to_end() {
Ok(s) => str::from_utf8(s.as_slice()).expect("this shouldn't happen").to_string(),
Err(e) => "".to_string(),
};
println!("ugh {}", contents.to_string());
}
【问题讨论】: