Skip to content

Commit 996ba1d

Browse files
committed
Auto merge of #29309 - rjbs:doc-comment-sections, r=alexcrichton
As displayed before this commit, I found the book confusing in its explanation of `#`-led comments in `rust` blocks. Possibly the biggest confusion was because the many-dashes construct does not become an HR element in the Markdown translator used, so things were not being properly set off. This change should more clearly show the as-rendered content as rendered, and the as-coded content as code.
2 parents cb591e5 + 5061133 commit 996ba1d

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

src/doc/trpl/documentation.md

+35-37
Original file line numberDiff line numberDiff line change
@@ -269,73 +269,71 @@ documentation comment, I need to add a little function definition below
269269
it. At the same time, it's just there to satisfy the compiler, so hiding
270270
it makes the example more clear. You can use this technique to explain
271271
longer examples in detail, while still preserving the testability of your
272-
documentation. For example, this code:
273-
274-
```rust
275-
let x = 5;
276-
let y = 6;
277-
println!("{}", x + y);
278-
```
279-
280-
Here's an explanation, rendered:
281-
282-
-------------------------------------------------------------------------------
272+
documentation.
283273

284-
First, we set `x` to five:
274+
For example, imagine that we wanted to document this code:
285275

286276
```rust
287277
let x = 5;
288-
# let y = 6;
289-
# println!("{}", x + y);
290-
```
291-
292-
Next, we set `y` to six:
293-
294-
```rust
295-
# let x = 5;
296278
let y = 6;
297-
# println!("{}", x + y);
298-
```
299-
300-
Finally, we print the sum of `x` and `y`:
301-
302-
```rust
303-
# let x = 5;
304-
# let y = 6;
305279
println!("{}", x + y);
306280
```
307281

308-
-------------------------------------------------------------------------------
309-
310-
Here's the same explanation, in raw text:
311-
312-
-------------------------------------------------------------------------------
282+
We might want the documentation to end up looking like this:
313283

314284
> First, we set `x` to five:
315285
>
316-
> ```text
286+
> ```rust
317287
> let x = 5;
318288
> # let y = 6;
319289
> # println!("{}", x + y);
320290
> ```
321291
>
322292
> Next, we set `y` to six:
323293
>
324-
> ```text
294+
> ```rust
325295
> # let x = 5;
326296
> let y = 6;
327297
> # println!("{}", x + y);
328298
> ```
329299
>
330300
> Finally, we print the sum of `x` and `y`:
331301
>
332-
> ```text
302+
> ```rust
333303
> # let x = 5;
334304
> # let y = 6;
335305
> println!("{}", x + y);
336306
> ```
337307
338-
-------------------------------------------------------------------------------
308+
To keep each code block testable, we want the whole program in each block, but
309+
we don't want the reader to see every line every time. Here's what we put in
310+
our source code:
311+
312+
```text
313+
First, we set `x` to five:
314+
315+
```text
316+
let x = 5;
317+
# let y = 6;
318+
# println!("{}", x + y);
319+
```
320+
321+
Next, we set `y` to six:
322+
323+
```text
324+
# let x = 5;
325+
let y = 6;
326+
# println!("{}", x + y);
327+
```
328+
329+
Finally, we print the sum of `x` and `y`:
330+
331+
```text
332+
# let x = 5;
333+
# let y = 6;
334+
println!("{}", x + y);
335+
```
336+
```
339337
340338
By repeating all parts of the example, you can ensure that your example still
341339
compiles, while only showing the parts that are relevant to that part of your

0 commit comments

Comments
 (0)