Macro fn_block::fn_block[][src]

macro_rules! fn_block {
    ($return_type:ty : $body:block) => { ... };
    ($body:block) => { ... };
}
Deprecated since 0.2.0

: Please use fn_expr instead, since it can also be used to wrap blocks. Blocks are expressions as well.

NOTE: This macro has been deprecated! Use fn_expr instead

Intro

This macro wraps a given rust code block into a closure and directly calls the closure. Optionally the return type of the closure can be specified first and separeted with a colon from the body block.

Example without return type:

let o = Some("Foobar");
let s = fn_expr!{ o?.get(0..3)?.to_lowercase().into_some() };
assert_eq!("foo", s.unwrap());

Example with return type:

use std::str::from_utf8;
use std::error::Error;
struct ConvertErr();
impl <T: Error> From<T> for ConvertErr {
    fn from(_: T) -> ConvertErr {ConvertErr()}
}
let bytes : &[u8] = &[0x0020,0x0034,0x0032];
let res_int = fn_block!{Result<u32,ConvertErr>: {
    let str = from_utf8(bytes)?.trim();
    str.parse::<u32>()?.into_ok()
}}.unwrap_or(0u32);
assert_eq!(res_int, 42);

Note that the examples use the traits IntoSome and IntoOk, Defined in this crate.