1 # Domain Specific Languages (DSLs)
3 A DSL is a mini "language" embedded in a Rust macro. It is completely valid
4 Rust because the macro system expands into normal Rust constructs, but it looks
5 like a small language. This allows you to define concise or intuitive syntax for
6 some special functionality (within bounds).
8 Suppose that I want to define a little calculator API. I would like to supply
9 an expression and have the output printed to console.
12 macro_rules! calculate {
15 let val: usize = $e; // Force types to be integers
16 println!("{} = {}", stringify!{$e}, val);
23 eval 1 + 2 // hehehe `eval` is _not_ a Rust keyword!
27 eval (1 + 2) * (3 / 4)
39 This was a very simple example, but much more complex interfaces have been
40 developed, such as [`lazy_static`](https://crates.io/crates/lazy_static) or
41 [`clap`](https://crates.io/crates/clap).
43 Also, note the two pairs of braces in the macro. The outer ones are
44 part of the syntax of `macro_rules!`, in addition to `()` or `[]`.