Implemented in #45837
New APIs in std::fs:
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> { … }
pub fn read_string<P: AsRef<Path>>(path: P) -> io::Result<String> { … }
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> { ... }
(read_string is based on read_to_string and so returns an error on non-UTF-8 content.)
Before:
use std::fs::File;
use std::io::Read;
let mut bytes = Vec::new();
File::open(filename)?.read_to_end(&mut bytes)?;
do_something_with(bytes)
After:
use std::fs;
do_something_with(fs::read(filename)?)
Implemented in #45837
New APIs in
std::fs:(
read_stringis based onread_to_stringand so returns an error on non-UTF-8 content.)Before:
After: