@@ -42,7 +42,7 @@ Documentation comments are written in Markdown.
42
42
Rust keeps track of these comments, and uses them when generating
43
43
documentation. This is important when documenting things like enums:
44
44
45
- ```
45
+ ``` rust
46
46
/// The `Option` type. See [the module level documentation](../) for more.
47
47
enum Option <T > {
48
48
/// No value
@@ -80,15 +80,15 @@ thing after that last comment.
80
80
81
81
Anyway, let's cover each part of this comment in detail:
82
82
83
- ```
83
+ ``` rust
84
84
/// Constructs a new `Rc<T>`.
85
85
# fn foo () {}
86
86
```
87
87
88
88
The first line of a documentation comment should be a short summary of its
89
89
functionality. One sentence. Just the basics. High level.
90
90
91
- ```
91
+ ``` rust
92
92
///
93
93
/// Other details about constructing `Rc<T>`s, maybe describing complicated
94
94
/// semantics, maybe additional options, all kinds of stuff.
@@ -101,7 +101,7 @@ we could have added more explanation in a new paragraph.
101
101
102
102
#### Special sections
103
103
104
- ```
104
+ ``` rust
105
105
/// # Examples
106
106
# fn foo () {}
107
107
```
@@ -110,7 +110,7 @@ Next, are special sections. These are indicated with a header, `#`. There
110
110
are three kinds of headers that are commonly used. They aren't special syntax,
111
111
just convention, for now.
112
112
113
- ```
113
+ ``` rust
114
114
/// # Panics
115
115
# fn foo () {}
116
116
```
@@ -120,7 +120,7 @@ usually indicated by panics, which kill the whole current thread at the very
120
120
least. If your function has a non-trivial contract like this, that is
121
121
detected/enforced by panics, documenting it is very important.
122
122
123
- ```
123
+ ``` rust
124
124
/// # Failures
125
125
# fn foo () {}
126
126
```
@@ -130,15 +130,15 @@ conditions under which it returns `Err(E)` is a nice thing to do. This is
130
130
slightly less important than ` Panics ` , because failure is encoded into the type
131
131
system, but it's still a good thing to do.
132
132
133
- ```
133
+ ``` rust
134
134
/// # Safety
135
135
# fn foo () {}
136
136
```
137
137
138
138
If your function is ` unsafe ` , you should explain which invariants the caller is
139
139
responsible for upholding.
140
140
141
- ```
141
+ ``` rust
142
142
/// # Examples
143
143
///
144
144
/// ```
@@ -154,7 +154,7 @@ method, and your users will love you for it. These examples go inside of
154
154
code block annotations, which we'll talk about in a moment, and can have
155
155
more than one section:
156
156
157
- ```
157
+ ``` rust
158
158
/// # Examples
159
159
///
160
160
/// Simple `&str` patterns:
@@ -179,7 +179,7 @@ Let's discuss the details of these code blocks.
179
179
180
180
To write some Rust code in a comment, use the triple graves:
181
181
182
- ```
182
+ ``` rust
183
183
/// ```
184
184
/// println!("Hello, world");
185
185
/// ```
@@ -188,7 +188,7 @@ To write some Rust code in a comment, use the triple graves:
188
188
189
189
If you want something that's not Rust code, you can add an annotation:
190
190
191
- ```
191
+ ``` rust
192
192
/// ```c
193
193
/// printf("Hello, world\n");
194
194
/// ```
@@ -208,7 +208,7 @@ generate the documentation.
208
208
209
209
Let's discuss our sample example documentation:
210
210
211
- ```
211
+ ``` rust
212
212
/// ```
213
213
/// println!("Hello, world");
214
214
/// ```
@@ -219,7 +219,7 @@ You'll notice that you don't need a `fn main()` or anything here. `rustdoc` will
219
219
automatically add a main() wrapper around your code, and in the right place.
220
220
For example:
221
221
222
- ```
222
+ ``` rust
223
223
/// ```
224
224
/// use std::rc::Rc;
225
225
///
@@ -230,7 +230,7 @@ For example:
230
230
231
231
This will end up testing:
232
232
233
- ```
233
+ ``` rust
234
234
fn main () {
235
235
use std :: rc :: Rc ;
236
236
let five = Rc :: new (5 );
@@ -259,7 +259,7 @@ with `///` we've been talking about? The raw text:
259
259
260
260
looks different than the output:
261
261
262
- ```
262
+ ``` rust
263
263
/// Some documentation.
264
264
# fn foo () {}
265
265
```
@@ -274,7 +274,7 @@ it makes the example more clear. You can use this technique to explain
274
274
longer examples in detail, while still preserving the testability of your
275
275
documentation. For example, this code:
276
276
277
- ```
277
+ ``` rust
278
278
let x = 5 ;
279
279
let y = 6 ;
280
280
println! (" {}" , x + y );
@@ -284,23 +284,23 @@ Here's an explanation, rendered:
284
284
285
285
First, we set ` x ` to five:
286
286
287
- ```
287
+ ``` rust
288
288
let x = 5 ;
289
289
# let y = 6 ;
290
290
# println! (" {}" , x + y );
291
291
```
292
292
293
293
Next, we set ` y ` to six:
294
294
295
- ```
295
+ ``` rust
296
296
# let x = 5 ;
297
297
let y = 6 ;
298
298
# println! (" {}" , x + y );
299
299
```
300
300
301
301
Finally, we print the sum of ` x ` and ` y ` :
302
302
303
- ```
303
+ ``` rust
304
304
# let x = 5 ;
305
305
# let y = 6 ;
306
306
println! (" {}" , x + y );
@@ -340,7 +340,7 @@ explanation.
340
340
341
341
Here’s an example of documenting a macro:
342
342
343
- ```
343
+ ```rust
344
344
/// Panic with a given message unless an expression evaluates to true.
345
345
///
346
346
/// # Examples
@@ -388,7 +388,7 @@ but with a binary, there’s nothing to link to.
388
388
There are a few more annotations that are useful to help ` rustdoc ` do the right
389
389
thing when testing your code:
390
390
391
- ```
391
+ ``` rust
392
392
/// ```ignore
393
393
/// fn foo() {
394
394
/// ```
@@ -400,7 +400,7 @@ what you want, as it's the most generic. Instead, consider annotating it
400
400
with ` text ` if it's not code, or using ` # ` s to get a working example that
401
401
only shows the part you care about.
402
402
403
- ```
403
+ ``` rust
404
404
/// ```should_panic
405
405
/// assert!(false);
406
406
/// ```
@@ -410,7 +410,7 @@ only shows the part you care about.
410
410
` should_panic ` tells ` rustdoc ` that the code should compile correctly, but
411
411
not actually pass as a test.
412
412
413
- ```
413
+ ``` rust
414
414
/// ```no_run
415
415
/// loop {
416
416
/// println!("Hello, world");
@@ -427,7 +427,7 @@ which you would want to make sure compile, but might run in an infinite loop!
427
427
428
428
Rust has another kind of doc comment, ` //! ` . This comment doesn't document the next item, but the enclosing item. In other words:
429
429
430
- ```
430
+ ``` rust
431
431
mod foo {
432
432
// ! This is documentation for the `foo` module.
433
433
// !
@@ -440,7 +440,7 @@ mod foo {
440
440
This is where you'll see ` //! ` used most often: for module documentation. If
441
441
you have a module in ` foo.rs ` , you'll often open its code and see this:
442
442
443
- ```
443
+ ``` rust
444
444
// ! A module for using `foo`s.
445
445
// !
446
446
// ! The `foo` module contains a lot of useful functionality blah blah blah
@@ -461,7 +461,7 @@ are written in Markdown, they're often `.md` files.
461
461
When you write documentation in Markdown files, you don't need to prefix
462
462
the documentation with comments. For example:
463
463
464
- ```
464
+ ``` rust
465
465
/// # Examples
466
466
///
467
467
/// ```
@@ -499,7 +499,7 @@ This `%` line needs to be the very first line of the file.
499
499
500
500
At a deeper level, documentation comments are sugar for documentation attributes:
501
501
502
- ```
502
+ ``` rust
503
503
/// this
504
504
# fn foo () {}
505
505
@@ -509,7 +509,7 @@ At a deeper level, documentation comments are sugar for documentation attributes
509
509
510
510
are the same, as are these:
511
511
512
- ```
512
+ ``` rust
513
513
// ! this
514
514
515
515
#![doc= " /// this" ]
@@ -546,7 +546,7 @@ pub use foo::bar;
546
546
You can control a few aspects of the HTML that ` rustdoc ` generates through the
547
547
` #![doc] ` version of the attribute:
548
548
549
- ```
549
+ ``` rust
550
550
#![doc(html_logo_url = " http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png" ,
551
551
html_favicon_url = " http://www.rust-lang.org/favicon.ico" ,
552
552
html_root_url = " http://doc.rust-lang.org/" )];
0 commit comments