This is the code I have:
use std::fs::File;
const INPUT_FILE_PATH: &str = "./data/input";
fn main() {
let file = match File::open(INPUT_FILE_PATH) {
Ok(file) => file,
Err(error) => println!("Error opening input file: {}", error),
};
}
When I run cargo run
, I get the following error:
error[E0308]: `match` arms have incompatible types
--> src/main.rs:8:23
|
6 | let file = match File::open(INPUT_FILE_PATH) {
| ________________-
7 | | Ok(file) => file,
| | ---- this is found to be of type `File`
8 | | Err(error) => println!("Error opening input file: {}", error),
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `File`, found `()`
9 | | };
| |_____- `match` arms have incompatible types
|
I don't understand the error message. I found out by accident that if I change the println!
to panic!
the program compiles without problems, but I can't for the life of me understand why println!
doesn't work.
Why is it that println!
gives an error, when panic!
doesn't?