Skip to content

Commit ec1c711

Browse files
committed
Simplify introductory examples
1 parent 1ffc0d4 commit ec1c711

2 files changed

Lines changed: 65 additions & 31 deletions

File tree

README.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,48 @@ int main(int argc, char **argv) {
5656

5757
## Composition
5858

59-
[`docstr!`](https://docs.rs/docstr/0.4.9/docstr/macro.docstr.html) can pass the generated string to any macro:
59+
[`docstr!`](https://docs.rs/docstr/0.4.9/docstr/macro.docstr.html) can pass the generated string to any macro. This example shows the string being forwarded to the [`format!`](https://doc.rust-lang.org/stable/alloc/macro.format.html) macro:
6060

6161
```rust
62-
use docstr::docstr;
63-
64-
let age = 21;
6562
let name = "Bob";
66-
let colors = ["red", "green", "blue"];
63+
let age = 21;
6764

6865
let greeting: String = docstr!(format!
69-
//^^^^^^^ the generated string is passed to `format!`
70-
// as the 1st argument
7166
/// Hello, my name is {name}.
72-
/// I am {age} years old!
73-
///
74-
/// My favorite color is: {}
67+
/// I am {} years old!
68+
age
69+
);
70+
71+
assert_eq!(greeting, "\
72+
Hello, my name is Bob.
73+
I am 21 years old!");
74+
```
75+
76+
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.
7577

76-
// anything after the doc comments is passed directly at the end
77-
colors[1]
78+
### How composition works
79+
80+
If the first argument to `docstr!` is a path to a macro, that macro will be called. This invocation:
81+
82+
```rust
83+
let greeting: String = docstr!(format!
84+
/// Hello, my name is {name}.
85+
/// I am {} years old!
86+
age
7887
);
79-
//^ above expands to: format!("...", colors[1])
88+
```
89+
90+
Is equivalent to this:
8091

81-
assert_eq!(greeting, "Hello, my name is Bob.\nI am 21 years old!\n\nMy favorite color is: green");
92+
```rust
93+
let greeting: String = format!("\
94+
Hello, my name is {name}.
95+
I am {} years old!"
96+
age,
97+
);
8298
```
8399

84-
Injecting arguments before the generated string is also possible.
100+
You can inject arguments before the format string:
85101

86102
```rust
87103
docstr!(write! w

src/lib.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,50 @@
3939
//!
4040
//! # Composition
4141
//!
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:
4343
//!
44-
//! ```rust
45-
//! use docstr::docstr;
46-
//!
47-
//! let age = 21;
44+
//! ```
45+
//! # use docstr::docstr;
4846
//! let name = "Bob";
49-
//! let colors = ["red", "green", "blue"];
47+
//! let age = 21;
5048
//!
5149
//! let greeting: String = docstr!(format!
52-
//! //^^^^^^^ the generated string is passed to `format!`
53-
//! // as the 1st argument
5450
//! /// 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+
//! ```
5859
//!
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
6172
//! );
62-
//! //^ above expands to: format!("...", colors[1])
73+
//! ```
74+
//!
75+
//! Is equivalent to this:
6376
//!
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+
//! );
6583
//! ```
6684
//!
67-
//! Injecting arguments before the generated string is also possible.
85+
//! You can inject arguments before the format string:
6886
//!
6987
//! ```rust
7088
//! # let mut w = String::new();

0 commit comments

Comments
 (0)