Closure build fail: consider giving a type


#1

Could somebody help? How to fix this build error?

use std::fs::File;

fn main() {
    let tmp = "tmp.zip";

    let save_to_tmp_file = || {
        let mut tmp_zip = File::create(tmp)?;
        Ok(())
    };

    save_to_tmp_file();
}

The build error:

  |
6 |     let save_to_tmp_file = || {
  |         ---------------- consider giving `save_to_tmp_file` a type
7 |         let mut tmp_zip = File::create(tmp)?;
  |                           ^^^^^^^^^^^^^^^^^^ cannot infer type for `_`

The Rust Playground code is here.


#2
let save_to_tmp_file = || -> Result<(), std::io::Error> {
        let mut tmp_zip = File::create(tmp)?;
        Ok(())
    };

or

let save_to_tmp_file = || {
        let mut tmp_zip = File::create(tmp)?;
        Ok::<_, std::io::Error>(())
    };

#3

Thanks a lot :clap::clap::clap: