【Rust】宏DSL

环境

  • Rust 1.56.1
  • VSCode 1.61.2

概念

参考:https://doc.rust-lang.org/rust-by-example/macros/dsl.html

示例

DSL 表示 Domain Specific Language,领域特定语言。

main.rs

macro_rules! calculate {
    (eval $e:expr) => {
        {
            let val: usize = $e; // Force types to be integers
            println!("{} = {}", stringify!{$e}, val);
        }
    };
}

fn main() {
    calculate! {
        eval 1 + 2 // hehehe `eval` is _not_ a Rust keyword!
    }

    calculate! {
        eval (1 + 2) * (3 / 4)
    }
}

总结

了解了 Rust 中使用宏定义领域特定语言。

附录

原文地址:https://www.cnblogs.com/jiangbo44/p/15727404.html