Reputation: 133
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) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Upvotes: 0
Views: 350
Reputation: 430791
You have multiple overlapping issues. Whenever debugging a programming problem, it helps to create a Minimal, Complete Verifiable Example.
Start by commenting out match file.read_to_string(&mut s) { /* ... */ }
. Then you will get another error:
error[E0282]: type annotations needed
--> src/main.rs:15:17
|
15 | let mut v = Vec::new();
| ----- ^^^^^^^^ cannot infer type for `T`
| |
| consider giving `v` a type
Comment out that line too, giving:
error[E0004]: non-exhaustive patterns: `Ok(_)` not covered
--> src/main.rs:6:22
|
6 | let file = match std::fs::File::open(&path) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `Ok(_)` not covered
This is your real issue. Result
is an enum with two values, Ok
and Err
. You have to handle all variants in a match.
In this case, it's easiest to use unwrap_or_else
:
let file = std::fs::File::open("input.txt").unwrap_or_else(|e| {
panic!(
"Failed to read file {}: {}",
path.display(),
e.description()
)
});
You can remove the unused vector and apply the same unwrap_or_else
to the other failure case. You then need to:
std::io::Read
.file
as mutable.You can also:
{}
.File::open
.use std::io::Read;
fn main() {
let path = "input.txt";
let mut file = std::fs::File::open(path).unwrap_or_else(|e| {
panic!("Failed to read file {}: {}", path, e);
});
let mut s = String::new();
file.read_to_string(&mut s).unwrap_or_else(|e| {
panic!("Failed to read file contents: {}", e);
});
println!("{}", s);
}
Compare your code against What's the de-facto way of reading and writing files in Rust 1.x? as well.
Upvotes: 4