Removing warnings in code

warning: unused `std::result::Result` that must be used
  --> src/bin/sort.rs:83:17
   |
83 |                 wtr.write_record(&rows[j]); 
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is the warning which I got while running the program. Could you please help me to resolve this warning?

I'm guessing that line returns Result

it'll be an Ok(Something) or an Error(some_error). You need to do something with it

let some_result = wtr.write_record(&rows[j]);
match some_result {
    Ok(val) => {do something with val here},
    Err(err) => {do something with error here}
}

It's probably best to check the return value, like @zireael9797 suggested. If you're sure you don't care about the return value you can ignore the value:

// explicitly ignore return value
let _ = wtr.write_record(&rows[j]);

Or panic if there's an error:

wtr.write_record(&rows[j]).unwrap();

You can also ignore the result with the .ok() method, like wtr.write_record(&rows[j]).ok(). It's maybe more concise, but perhaps makes it less obvious what you're doing. Technically it converts a Result into a Option, losing the error details.

1 Like

In general, you should handle errors instead of ignoreing them, unless there're some significant reason to not do so. There're several ways/hacks to ignore this kind warnings, but I'd expect a dedicated line of comments to describe why you ignore error here for every such occurence.

If you don't have much concern what to do on error, just .unwrap()-ing the Result would be better than just ignoring it, since it would make you think when it actually happens, instead of let some other code behave incorrectly and let you debug the entire code flow.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.