Skip to content

Commit fbc823d

Browse files
committed
Document how to document macros
Fixes #23571
1 parent ecf8c64 commit fbc823d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/doc/trpl/documentation.md

+35
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,41 @@ By repeating all parts of the example, you can ensure that your example still
333333
compiles, while only showing the parts that are relevant to that part of your
334334
explanation.
335335
336+
### Documenting macros
337+
338+
Here’s an example of documenting a macro:
339+
340+
```
341+
/// Panic with a given message unless an expression evaluates to true.
342+
///
343+
/// # Examples
344+
///
345+
/// ```
346+
/// # #[macro_use] extern crate foo;
347+
/// # fn main() {
348+
/// panic_unless!(1 + 1 == 2, “Math is broken.”);
349+
/// # }
350+
/// ```
351+
///
352+
/// ```should_fail
353+
/// # #[macro_use] extern crate foo;
354+
/// # fn main() {
355+
/// panic_unless!(true == false, “I’m broken.”);
356+
/// # }
357+
/// ```
358+
#[macro_export]
359+
macro_rules! panic_unless {
360+
($condition:expr, $($rest:expr),+) => ({ if ! $condition { panic!($($rest),+); } });
361+
}
362+
```
363+
364+
You’ll note three things: we need to add our own `extern crate` line, so that
365+
we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
366+
`main()` as well. Finally, a judicious use of `#` to comment out those two
367+
things, so they don’t show up in the output.
368+
369+
### Running documentation tests
370+
336371
To run the tests, either
337372
338373
```bash

0 commit comments

Comments
 (0)