4

In the following example, taken from nom, the string __impl appears in the first pattern. What is this element in terms of formal syntax and why is it there?

I'm under the impression that the literal __impl in the pattern and the subsequent invocations are used as a dummy-string during pattern-matching, to separate the internally used patterns from the public ones. However, the "internal" match arm is hidden from rustdoc as well. Does the __impl-part have any more significance to Rust?

#[macro_export]
macro_rules! map(
  // Internal parser, do not use directly
  (__impl $i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => (
    ...
  );
  ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => (
    map!(__impl $i, $submac!($($args)*), $g);
  );
  ($i:expr, $f:expr, $g:expr) => (
    map!(__impl $i, call!($f), $g);
  );
);
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
user2722968
  • 13,636
  • 2
  • 46
  • 67

1 Answers1

4

I'm under the impression that the literal __impl in the pattern and the subsequent invocations are used as a dummy-string during pattern-matching, to separate the internally used patterns from the public ones.

That is correct. It's an internal implementation detail that users should not know of or use. However, macros have to be public because they are ultimately expanded "by the user" at the usage site.

However, the "internal" match arm is hidden from rustdoc as well.

I don't know what you mean, that arm is in the documentation:

__impl in docs

See also:

Does the __impl-part have any more significance to Rust?

No. Many times you'll see this pattern prepended with another unlikely-to-occur sigil like @ or ~.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366