2024-02-16 20:02:50 +00:00
|
|
|
//@ run-pass
|
2015-03-22 20:13:15 +00:00
|
|
|
|
2015-01-02 22:44:21 +00:00
|
|
|
macro_rules! mypat {
|
2014-05-19 22:14:23 +00:00
|
|
|
() => (
|
|
|
|
Some('y')
|
|
|
|
)
|
2015-01-02 22:44:21 +00:00
|
|
|
}
|
2014-05-19 22:14:23 +00:00
|
|
|
|
2015-01-02 22:44:21 +00:00
|
|
|
macro_rules! char_x {
|
2014-05-19 22:14:23 +00:00
|
|
|
() => (
|
|
|
|
'x'
|
|
|
|
)
|
2015-01-02 22:44:21 +00:00
|
|
|
}
|
2014-05-19 22:14:23 +00:00
|
|
|
|
2015-01-02 22:44:21 +00:00
|
|
|
macro_rules! some {
|
2014-05-19 22:14:23 +00:00
|
|
|
($x:pat) => (
|
|
|
|
Some($x)
|
|
|
|
)
|
2015-01-02 22:44:21 +00:00
|
|
|
}
|
2014-05-19 22:14:23 +00:00
|
|
|
|
2015-01-02 22:44:21 +00:00
|
|
|
macro_rules! indirect {
|
2014-05-19 22:14:23 +00:00
|
|
|
() => (
|
|
|
|
some!(char_x!())
|
|
|
|
)
|
2015-01-02 22:44:21 +00:00
|
|
|
}
|
2014-05-19 22:14:23 +00:00
|
|
|
|
2015-01-02 22:44:21 +00:00
|
|
|
macro_rules! ident_pat {
|
2014-05-19 22:14:23 +00:00
|
|
|
($x:ident) => (
|
|
|
|
$x
|
|
|
|
)
|
2015-01-02 22:44:21 +00:00
|
|
|
}
|
2014-05-19 22:14:23 +00:00
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
fn f(c: Option<char>) -> usize {
|
2014-05-19 22:14:23 +00:00
|
|
|
match c {
|
|
|
|
Some('x') => 1,
|
|
|
|
mypat!() => 2,
|
|
|
|
_ => 3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2015-03-03 08:42:26 +00:00
|
|
|
assert_eq!(1, f(Some('x')));
|
|
|
|
assert_eq!(2, f(Some('y')));
|
|
|
|
assert_eq!(3, f(None));
|
2014-05-19 22:14:23 +00:00
|
|
|
|
2015-01-25 21:05:03 +00:00
|
|
|
assert_eq!(1, match Some('x') {
|
|
|
|
Some(char_x!()) => 1,
|
|
|
|
_ => 2,
|
2014-05-19 22:14:23 +00:00
|
|
|
});
|
|
|
|
|
2015-01-25 21:05:03 +00:00
|
|
|
assert_eq!(1, match Some('x') {
|
|
|
|
some!(char_x!()) => 1,
|
|
|
|
_ => 2,
|
2014-05-19 22:14:23 +00:00
|
|
|
});
|
|
|
|
|
2015-01-25 21:05:03 +00:00
|
|
|
assert_eq!(1, match Some('x') {
|
|
|
|
indirect!() => 1,
|
|
|
|
_ => 2,
|
2014-05-19 22:14:23 +00:00
|
|
|
});
|
|
|
|
|
2015-01-25 21:05:03 +00:00
|
|
|
assert_eq!(3, {
|
|
|
|
let ident_pat!(x) = 2;
|
|
|
|
x+1
|
2014-05-19 22:14:23 +00:00
|
|
|
});
|
|
|
|
}
|