This is perhaps not the best way to do this however for sanity sake and to better understand the language what would the return type be for the function below: I can't seem to figure it out.
fn unpack_file(parser: &SomeProcessor, path: std::path::PathBuf) -> ??? {
let file = OpenOptions::new()
.read(true)
.open(path.as_path()).unwrap();
let reader = io::BufReader::new(file);
let path_str = path.to_str().unwrap().to_string();
let stream = reader.lines().enumerate().map(|(i,line)| {
line.map_err(|e| LineError(SomeError::IoError(e), i,path_str.to_owned()))
.and_then(|record: String| {
parser.process(&record).map_err(|e| LineError(e, i,path_str.to_owned()))
})
});
stream
}
Compiler says:
187 | stream
| ^^^^^^ expected struct `???`, found struct `std::iter::Map`
|
= note: expected type `???'
found type `std::iter::Map<std::iter::Enumerate<std::io::Lines<std::io::BufReader<std::fs::File>>>, [closure@src/main.rs:181:49: 186:6 path_str:_, parser:_]>`
found type std::iter::Map<std::iter::Enumerate<std::io::Lines<std::io::BufReader<std::fs::File>>>, [closure@src/main.rs:181:49: 186:6 path_str:_, parser:_]>
Yeah, that's its type - you can't write it down because you can't write down the type of a closure. But one possible signature for the function would be (using the impl Trait syntax):
Probably? Also, you need to be on nightly and add #![feature(conservative_impl_trait)] to the top of your main.rs or lib.rs for impl Trait to work right now.
You need to use the nightly channel to use impl Trait right now, because the details and implementation are still being worked out. See the book for more information: