2 个版本
使用旧的 Rust 2015
0.1.2 | 2016 年 9 月 23 日 |
---|---|
0.1.1 | 2016 年 9 月 23 日 |
0.1.0 |
|
#116 in #panic
4KB
match_cast
这是一个实现不同类型匹配的最小包。
用法
#[macro_use]
extern crate match_cast;
use std::panic;
fn main() {
let res = panic::catch_unwind(|| {
panic!("Oh no!");
});
let any = res.unwrap_err();
let result = match_cast!( any {
val as Option<u8> => {
format!("Option<u8> = {:?}", val)
},
val as String => {
format!("String = {:?}", val)
},
val as &'static str => {
format!("&'static str = {:?}", val)
},
});
assert_eq!(result.unwrap(), "&'static str = \"Oh no!\"");
}
要使用模式,有 match_down
宏
#[macro_use]
extern crate match_cast;
use std::any::Any;
struct Bar {
x: u8,
}
struct Foo {
x: u8,
}
fn main() {
let any: Box<Any> = Box::new(Foo { x: 45 });
let result = match_down!( any {
Bar { x } => { x },
Foo { x } => { x },
});
assert_eq!(result.unwrap(), 45);
}