Читаем Rust by Example полностью

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

$ rustc --test dry.rs && ./dry

running 3 tests

test test::mul_assign ... ok

test test::add_assign ... ok

test test::sub_assign ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured

<p id="domain_specific_languages_dsls"><strong><a l:href="#domain_specific_languages_dsls">Domain Specific Languages (DSLs)</a></strong></p>

A DSL is a mini "language" embedded in a Rust macro. It is completely valid Rust because the macro system expands into normal Rust constructs, but it looks like a small language. This allows you to define concise or intuitive syntax for some special functionality (within bounds).

Suppose that I want to define a little calculator API. I would like to supply an expression and have the output printed to console.

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)

}

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

1 + 2 = 3

(1 + 2) * (3 / 4) = 0

This was a very simple example, but much more complex interfaces have been developed, such as lazy_static or clap.

Also, note the two pairs of braces in the macro. The outer ones are part of the syntax of macro_rules!, in addition to () or [].

<p id="variadic_interfaces"><strong><a l:href="#variadic_interfaces">Variadic Interfaces</a></strong></p>

A variadic interface takes an arbitrary number of arguments. For example, println! can take an arbitrary number of arguments, as determined by the format string.

We can extend our calculate! macro from the previous section to be variadic:

macro_rules! calculate {

// The pattern for a single `eval`

(eval $e:expr) => {{

{

let val: usize = $e; // Force types to be integers

println!("{} = {}", stringify!{$e}, val);

}

}};

// Decompose multiple `eval`s recursively

(eval $e:expr, $(eval $es:expr),+) => {{

calculate! { eval $e }

calculate! { $(eval $es),+ }

}};

}

fn main() {

calculate! { // Look ma! Variadic `calculate!`!

eval 1 + 2,

eval 3 + 4,

eval (2 * 3) + 1

}

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

Перейти на страницу:

Похожие книги