|
39 | 39 | //! |
40 | 40 | //! # Composition |
41 | 41 | //! |
42 | | -//! [`docstr!`](crate::docstr) can pass the generated string to any macro: |
| 42 | +//! [`docstr!`](crate::docstr) can pass the generated string to any macro. This example shows the string being forwarded to the [`format!`] macro: |
43 | 43 | //! |
44 | | -//! ```rust |
45 | | -//! use docstr::docstr; |
46 | | -//! |
47 | | -//! let age = 21; |
| 44 | +//! ``` |
| 45 | +//! # use docstr::docstr; |
48 | 46 | //! let name = "Bob"; |
49 | | -//! let colors = ["red", "green", "blue"]; |
| 47 | +//! let age = 21; |
50 | 48 | //! |
51 | 49 | //! let greeting: String = docstr!(format! |
52 | | -//! //^^^^^^^ the generated string is passed to `format!` |
53 | | -//! // as the 1st argument |
54 | 50 | //! /// Hello, my name is {name}. |
55 | | -//! /// I am {age} years old! |
56 | | -//! /// |
57 | | -//! /// My favorite color is: {} |
| 51 | +//! /// I am {} years old! |
| 52 | +//! age |
| 53 | +//! ); |
| 54 | +//! |
| 55 | +//! assert_eq!(greeting, "\ |
| 56 | +//! Hello, my name is Bob. |
| 57 | +//! I am 21 years old!"); |
| 58 | +//! ``` |
58 | 59 | //! |
59 | | -//! // anything after the doc comments is passed directly at the end |
60 | | -//! colors[1] |
| 60 | +//! This is great because there's just a single macro, `docstr!`, that can do anything. No need for `docstr_format!`, `docstr_println!`, `docstr_write!`, etc. |
| 61 | +//! |
| 62 | +//! ## How composition works |
| 63 | +//! |
| 64 | +//! If the first argument to `docstr!` is a path to a macro, that macro will be called. This invocation: |
| 65 | +//! |
| 66 | +//! ``` |
| 67 | +//! # use docstr::docstr; |
| 68 | +//! let greeting: String = docstr!(format! |
| 69 | +//! /// Hello, my name is {name}. |
| 70 | +//! /// I am {} years old! |
| 71 | +//! age |
61 | 72 | //! ); |
62 | | -//! //^ above expands to: format!("...", colors[1]) |
| 73 | +//! ``` |
| 74 | +//! |
| 75 | +//! Is equivalent to this: |
63 | 76 | //! |
64 | | -//! assert_eq!(greeting, "Hello, my name is Bob.\nI am 21 years old!\n\nMy favorite color is: green"); |
| 77 | +//! ``` |
| 78 | +//! let greeting: String = format!("\ |
| 79 | +//! Hello, my name is {name}. |
| 80 | +//! I am {} years old!" |
| 81 | +//! age, |
| 82 | +//! ); |
65 | 83 | //! ``` |
66 | 84 | //! |
67 | | -//! Injecting arguments before the generated string is also possible. |
| 85 | +//! You can inject arguments before the format string: |
68 | 86 | //! |
69 | 87 | //! ```rust |
70 | 88 | //! # let mut w = String::new(); |
|
0 commit comments