Dolphin
Dolphin

Reputation: 38681

why get the this `Result` may be an `Err` variant, which should be handled warning when write to file in rust

I write some content into the file using this code in rust:

fn renderBody(w:&mut File,doc: &Vec<UserDictResponse>) {
    for word in doc {
        let word_content = format!("{}{}{}", r"\textbf{", word.word.to_string(),r"}");
        writeln!(w,"{}",word_content);
        writeln!(w,r"");
        let translate = format!("{}",word.translation.to_string());
        writeln!(w,"{}",translate);
        writeln!(w,r"");
        writeln!(w,r"\vspace{{12pt}}");
        writeln!(w,r"");
    }
}

when I compile this code, shows warning like this:

warning: unused `Result` that must be used
   --> src/service/word/user_dict_service.rs:116:9
    |
116 |         writeln!(w,r"\vspace{{12pt}}");
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this `Result` may be an `Err` variant, which should be handled
    = note: this warning originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: unused `Result` that must be used
   --> src/service/word/user_dict_service.rs:117:9
    |
117 |         writeln!(w,r"");
    |         ^^^^^^^^^^^^^^^^
    |
    = note: this `Result` may be an `Err` variant, which should be handled
    = note: this warning originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)

I should handle the Err when using write? I did not return any Result, what should I do to handle the result properly? I make a minimal example like this:

use std::fs::File;
use std::io::Write;
fn main() {
    let path = "/home/dolphin/demo.tex";
    let mut file = File::create(path).unwrap();
    writeln!(&mut file, r"\documentclass[12pt]{{book}}").unwrap();
}

when I compile, it did not have warning. This makes me confusing.

Upvotes: 0

Views: 1817

Answers (1)

Ry-
Ry-

Reputation: 224904

.unwrap() will cause your program to panic if it fails to write to the file, which is potentially a valid thing to do – how errors should be handled is really up to you. So if you want to panic on every error, you can do like you did in the minimal example:

fn renderBody(w: &mut File, doc: &Vec<UserDictResponse>) {
    for word in doc {
        let word_content = format!("{}{}{}", r"\textbf{", word.word.to_string(), r"}");
        writeln!(w, "{}", word_content).unwrap();
        writeln!(w, r"").unwrap();
        let translate = format!("{}", word.translation.to_string());
        writeln!(w, "{}", translate).unwrap();
        writeln!(w, r"").unwrap();
        writeln!(w, r"\vspace{{12pt}}").unwrap();
        writeln!(w, r"").unwrap();
    }
}

But:

I did not return any Result, what should I do to handle the result properly?

You could change to returning a Result, return Err any time there’s an error writing to the file, and leave error handling to the caller. Even if you want to panic, this might be the cleaner option:

fn renderBody(w: &mut File, doc: &Vec<UserDictResponse>) -> Result<(), io::Error> {
    for word in doc {
        let word_content = format!("{}{}{}", r"\textbf{", word.word.to_string(), r"}");
        writeln!(w, "{}", word_content)?;
        writeln!(w, r"")?;
        let translate = format!("{}", word.translation.to_string());
        writeln!(w, "{}", translate)?;
        writeln!(w, r"")?;
        writeln!(w, r"\vspace{{12pt}}")?;
        writeln!(w, r"")?;
    }
    Ok(())
}

and then renderBody(…).unwrap();.

Upvotes: 1

Related Questions