我正在尝试读取一个文件并将其作为UTF-8
std:string:String
返回,看起来content
是一个Result<collections::string::String, collections::vec::Vec<u8>>
,如果我理解了从尝试String::from_utf8(content)
得到的错误消息。
fn get_index_body () -> String {
let path = Path::new("../html/ws1.html");
let display = path.display();
let mut file = match File::open(&path) {
Ok(f) => f,
Err(err) => panic!("file error: {}", err)
};
let content = file.read_to_end();
println!("{} {}", display, content);
return String::new(); // how to turn into String (which is utf-8)
}
let s = String::from_utf8(content).unwrap();
这种方式在将内容转换为String
后返回它是一个好的选择吗?我指的是资源使用方面。 - Victory