]> git.lizzy.rs Git - rust.git/commitdiff
Document how to document macros
authorSteve Klabnik <steve@steveklabnik.com>
Sun, 22 Mar 2015 19:26:23 +0000 (15:26 -0400)
committerSteve Klabnik <steve@steveklabnik.com>
Sun, 22 Mar 2015 19:26:23 +0000 (15:26 -0400)
Fixes #23571

src/doc/trpl/documentation.md

index 8e5b3b6a7f0afa2f25c1b7938de9cf525d9ecd45..7300753cc66d0ea3363d9d638bc1a2d7c7199b63 100644 (file)
@@ -333,6 +333,41 @@ By repeating all parts of the example, you can ensure that your example still
 compiles, while only showing the parts that are relevant to that part of your
 explanation.
 
+### Documenting macros
+
+Here’s an example of documenting a macro:
+
+```
+/// Panic with a given message unless an expression evaluates to true.
+///
+/// # Examples
+///
+/// ```
+/// # #[macro_use] extern crate foo;
+/// # fn main() {
+/// panic_unless!(1 + 1 == 2, “Math is broken.”);
+/// # }
+/// ```
+///
+/// ```should_fail
+/// # #[macro_use] extern crate foo;
+/// # fn main() {
+/// panic_unless!(true == false, “I’m broken.”);
+/// # }
+/// ```
+#[macro_export]
+macro_rules! panic_unless {
+    ($condition:expr, $($rest:expr),+) => ({ if ! $condition { panic!($($rest),+); } });
+} 
+```
+
+You’ll note three things: we need to add our own `extern crate` line, so that
+we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
+`main()` as well. Finally, a judicious use of `#` to comment out those two
+things, so they don’t show up in the output.
+
+### Running documentation tests
+
 To run the tests, either
 
 ```bash