According to multiple sources, I believe this is the correct way to read a string from a file:
use std::error::Error;
fn main() {
let path = std::path::Path::new("input.txt");
let file = match std::fs::File::open(&path) {
Err(e) => {
panic!("Failed to read file {}: {}",
path.display(),
e.description())
}
};
let mut s = String::new();
let mut v = Vec::new();
match file.read_to_string(&mut s) {
Err(e) => panic!("Failed to read file contents: {}", e.description()),
}
println!("{}", s);
}
But this code produces an error using Rust 1.17.0 so I must be missing something:
error: the type of this value must be known in this context
--> src/main.rs:16:11
|
16 | match file.read_to_string(&mut s) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^