rust/src/test/run-pass/macro-pat.rs

75 lines
1.3 KiB
Rust
Raw Normal View History

2014-05-19 17:14:23 -05:00
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
macro_rules! mypat {
2014-05-19 17:14:23 -05:00
() => (
Some('y')
)
}
2014-05-19 17:14:23 -05:00
macro_rules! char_x {
2014-05-19 17:14:23 -05:00
() => (
'x'
)
}
2014-05-19 17:14:23 -05:00
macro_rules! some {
2014-05-19 17:14:23 -05:00
($x:pat) => (
Some($x)
)
}
2014-05-19 17:14:23 -05:00
macro_rules! indirect {
2014-05-19 17:14:23 -05:00
() => (
some!(char_x!())
)
}
2014-05-19 17:14:23 -05:00
macro_rules! ident_pat {
2014-05-19 17:14:23 -05:00
($x:ident) => (
$x
)
}
2014-05-19 17:14:23 -05:00
fn f(c: Option<char>) -> usize {
2014-05-19 17:14:23 -05:00
match c {
Some('x') => 1,
mypat!() => 2,
_ => 3,
}
}
pub fn main() {
assert_eq!(1, f(Some('x')));
assert_eq!(2, f(Some('y')));
assert_eq!(3, f(None));
2014-05-19 17:14:23 -05:00
2015-01-25 15:05:03 -06:00
assert_eq!(1, match Some('x') {
Some(char_x!()) => 1,
_ => 2,
2014-05-19 17:14:23 -05:00
});
2015-01-25 15:05:03 -06:00
assert_eq!(1, match Some('x') {
some!(char_x!()) => 1,
_ => 2,
2014-05-19 17:14:23 -05:00
});
2015-01-25 15:05:03 -06:00
assert_eq!(1, match Some('x') {
indirect!() => 1,
_ => 2,
2014-05-19 17:14:23 -05:00
});
2015-01-25 15:05:03 -06:00
assert_eq!(3, {
let ident_pat!(x) = 2;
x+1
2014-05-19 17:14:23 -05:00
});
}