Crates.io | try_into_opt |
lib.rs | try_into_opt |
version | 0.1.0 |
source | src |
created_at | 2016-02-19 03:10:51.936491 |
updated_at | 2016-02-19 03:10:51.936491 |
description | A macro like try!, but turns a Result into an Option, mainly for use in a filter_map |
homepage | |
repository | https://github.com/frostly/try_into_opt |
max_upload_size | |
id | 4211 |
size | 19,261 |
try_into_opt!
Like try!
, but turns a Result
into an Option
, mainly for use in a filter_map
. Provides
early return for errors like try!
does in map
.
try_into_opt!
is different from try_opt!, which
provides early return for functions that return an Option
. try_into_opt
operates on functions
that return a Result
but your code requires an option. This scenario may come up when doing IO
that can fail, but if it succeeds, needs to be filtered in some way.
Add this to your Cargo.toml
:
[dependencies]
try_into_opt = "0.1.0"
And to your crate:
#[macro_use]
extern crate try_into_opt;
#[macro_use]
extern crate try_into_opt;
const MAX_NUM: usize = 11usize;
fn num_range_filter(v: &[usize]) -> Result<Vec<usize>, String> {
v.iter()
.filter_map(|i| {
let i = try_into_opt!(validate_num(*i));
// do some filtering...
if i == 5 {
None
} else {
Some(Ok(i))
}
})
.collect()
}
fn validate_num(i: usize) -> Result<usize, String> {
if i > MAX_NUM {
Err(format!("{} is larger than the allowed max of {}", i, MAX_NUM))
} else {
Ok(i)
}
}
fn main() {
let nums = num_range_filter(&[1, 2, 3, 10, 12, 0]);
assert_eq!(nums.is_err(), true);
let nums = num_range_filter(&[1, 2, 5, 3, 5, 10, 0, 5]).unwrap();
assert_eq!(nums, [1, 2, 3, 10, 0]);
}
For more details, head over to the documentation.
This library is distributed under similar terms to Rust: dual-licensed under the MIT license and the Apache license (version 2.0).
See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.