rust/src/test/run-pass/macro-of-higher-order.rs
2016-07-19 20:22:20 +00:00

32 lines
947 B
Rust

// Copyright 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! higher_order {
(subst $lhs:tt => $rhs:tt) => ({
macro_rules! anon { $lhs => $rhs }
anon!(1_usize, 2_usize, "foo")
});
}
macro_rules! outer {
($x:expr; $fragment:ident) => {
macro_rules! inner { ($y:$fragment) => { $x + $y } }
}
}
fn main() {
let val = higher_order!(subst ($x:expr, $y:expr, $foo:expr) => (($x + $y, $foo)));
assert_eq!(val, (3, "foo"));
outer!(2; expr);
assert_eq!(inner!(3), 5);
}