I wrote this code while learning error handling:
#![allow(unused)]
use std::io;
use std::fs::File;
use std::io::Read;
fn main() {
openfile4();
}
fn openfile4() -> Result<String, std::io::Error> {
let result = File::open("hello.txt");
match result {
Ok(mut fileh) => {
let mut s = String::new();
match fileh.read_to_string(&mut s) {
Ok(_) => Ok(s),
Err(ef) => Err(ef),
}
},
eff => eff
}
}
this gives error that 'match arms have incompatible types' :
error[E0308]: `match` arms have incompatible types
--> src/main.rs:24:16
|
16 | match result {
| ------------ `match` arms have incompatible types
...
19 | / match fileh.read_to_string(&mut s) {
20 | | Ok(_) => Ok(s),
21 | | Err(ef) => Err(ef),
22 | | }
| |_____________- this is found to be of type `Result<String, std::io::Error>`
23 | },
24 | eff => eff
| ^^^ expected struct `String`, found struct `File`
|
= note: expected enum `Result<String, _>`
found enum `Result<File, _>`
error: aborting due to previous error
It doesn't make sense to me though - the eff => eff
is simply returning Err(std::io::Error)
right? I don't get the expected struct String, found struct File
message either.
I know that the error goes away by using Err(eff) => Err(eff)
, but the way I see it, the above should work as well.
Can someone please help me make sense of the error message above?