#io #optional #file-exists #error #fs-file #handle #found

deprecated io-result-optional

Provide an optional() method for io::Result

3 releases

0.1.3 Feb 2, 2022
0.1.2 Jan 12, 2019
0.1.1 Jan 11, 2019
0.1.0 Jan 11, 2019

#19 in #file-exists

25 downloads per month

MIT license

6KB

IoResultOptional

A trait for io::Result that adds a method making it easy to tell the difference between a file not found and another error, since a common practice is to handle a file if it exists.

docs Build Status

if let Some(input) = File::open("data").optional()? {
    // The data exists, so handle it ...
    // If it doesn't exist, it is just ignored
    // If there is another error, this function returns it.
}

lib.rs:

Provides a trait for std::io::Result that adds a method making it easy to tell the difference between a file not found and another error, since a common practice is to handle a file if it exists.

Examples

use io_result_optional::IoResultOptional;
use std::fs::File;

let config = File::open(".app.rc")
    .optional()?
    .map(readconfig)
    .unwrap_or_default();
use io_result_optional::IoResultOptional;
use std::fs::File;

if let Some(input) = File::open("data").optional()? {
    // The data exists, so handle it ...
    // If it doesn't exist, it is just ignored
    // If there is another error, this function returns it.
}

No runtime deps