Crates.io | unwrap_goto |
lib.rs | unwrap_goto |
version | 0.1.2 |
source | src |
created_at | 2022-07-29 10:32:03.532499 |
updated_at | 2022-08-09 03:32:28.738762 |
description | unwrap Option an Result or goto `return`/`cotinue`/`break` as you want. |
homepage | https://github.com/mu2019/unwrap_goto |
repository | https://github.com/mu2019/unwrap_goto |
max_upload_size | |
id | 634932 |
size | 9,297 |
unwrap Option an Result data. When data is wrap with Some or Ok, it will unwrap, otherwise can goto return
/cotinue
/break
as you want.
When none will return.
#[macro_use]
extern crate unwrap_goto;
fn do_none_is_return(m: &Vec<Option<i32>>) {
let mut k = 0;
for v in m {
println!("imcome:{:?}", v);
k = some_or_return!(v);
println!("wrap value:{:?}", k);
}
assert_eq!(k, 1);
0
}
fn none_is_return() {
let m = vec![Some(1), None, Some(1)];
let n = do_none_is_return(&m);
println!("return value:{:?}", n);
}
fn do_none_is_return(m: &Vec<Option<i32>>) -> i64 {
let mut k = 0;
for v in m {
println!("imcome:{:?}", v);
k = some_or_return!(v, 8);
println!("wrap value:{:?}", k);
}
assert_eq!(k, 1);
0
}
fn none_is_return() {
let m = vec![Some(1), None, Some(1)];
let n = do_none_is_return(&m);
println!("return value:{:?}", n);
}
use log::error;
fn call_when_err_return_log() {
let x: Result<i32, &str> = Err("Some error message");
ok_or_return!(x, (), error!("log error info.{:?}", x));
println!("anything print out");
}