Expand description
This crate provides a single macro to help skipping a error in a loop, possibly logging it.
For example, imagine you have some code like this.
ⓘ
for string_number in &["1", "2", "three", "4"] {
let number: u32 = match string_number.parse() {
Ok(n) => n,
Err(e) => continue,
};
}
Then you can use the macro skip_error!
to write like this.
ⓘ
for string_number in &["1", "2", "three", "4"] {
let number: u32 = skip_error!(string_number.parse());
}
Or even better, use the trait SkipError
that extends Iterator
and do
the following (essentially equivalent to Iterator::flatten()
but see
below for logging abilities).
ⓘ
use skip_error::SkipError;
let numbers: Vec<u32> = ["1", "2", "three", "4"]
.into_iter()
.map(|string_number| string_number.parse())
.skip_error()
.collect();
§Logging
If you want the error to be logged, you can use the feature log
or the
feature tracing
(see Features). See skip_error_and_log!
and SkipError::skip_error_and_log()
for more information.
§Features
log
: emit log message with the standardstd::log
macro. Disabled by default.tracing
: emit traces with thetracing::trace
macro. Disabled by default. If bothlog
andtracing
are enabled, thenlog
will be ignored sincetracing
is configured in a compatibility mode with standardlog
.
Macros§
- skip_
error skip_error
returns the value of aResult
or continues a loop.- skip_
error_ and_ debug skip_error_and_debug
returns the value of aResult
or log withtracing::Level::DEBUG
and continues the loop.- skip_
error_ and_ error skip_error_and_error
returns the value of aResult
or log withtracing::Level::ERROR
and continues the loop.- skip_
error_ and_ info skip_error_and_info
returns the value of aResult
or log withtracing::Level::INFO
and continues the loop.- skip_
error_ and_ log skip_error_and_log
returns the value of aResult
or log and continues the loop.- skip_
error_ and_ trace skip_error_and_trace
returns the value of aResult
or log withtracing::Level::TRACE
and continues the loop.- skip_
error_ and_ warn skip_error_and_warn
returns the value of aResult
or log withtracing::Level::WARN
and continues the loop.
Structs§
- Skip
Error Iter - An iterator that ignore errors
Traits§
- Skip
Error - Trait to extend any
Iterator
where theIterator::Item
is aResult
. This allows to skip errors and keep only theOk()
values.