@@ -42,7 +42,7 @@ Documentation comments are written in Markdown.
4242Rust keeps track of these comments, and uses them when generating
4343documentation. This is important when documenting things like enums:
4444
45- ```
45+ ``` rust
4646/// The `Option` type. See [the module level documentation](../) for more.
4747enum Option <T > {
4848 /// No value
@@ -80,15 +80,15 @@ thing after that last comment.
8080
8181Anyway, let's cover each part of this comment in detail:
8282
83- ```
83+ ``` rust
8484/// Constructs a new `Rc<T>`.
8585# fn foo () {}
8686```
8787
8888The first line of a documentation comment should be a short summary of its
8989functionality. One sentence. Just the basics. High level.
9090
91- ```
91+ ``` rust
9292///
9393/// Other details about constructing `Rc<T>`s, maybe describing complicated
9494/// semantics, maybe additional options, all kinds of stuff.
@@ -101,7 +101,7 @@ we could have added more explanation in a new paragraph.
101101
102102#### Special sections
103103
104- ```
104+ ``` rust
105105/// # Examples
106106# fn foo () {}
107107```
@@ -110,7 +110,7 @@ Next, are special sections. These are indicated with a header, `#`. There
110110are three kinds of headers that are commonly used. They aren't special syntax,
111111just convention, for now.
112112
113- ```
113+ ``` rust
114114/// # Panics
115115# fn foo () {}
116116```
@@ -120,7 +120,7 @@ usually indicated by panics, which kill the whole current thread at the very
120120least. If your function has a non-trivial contract like this, that is
121121detected/enforced by panics, documenting it is very important.
122122
123- ```
123+ ``` rust
124124/// # Failures
125125# fn foo () {}
126126```
@@ -130,15 +130,15 @@ conditions under which it returns `Err(E)` is a nice thing to do. This is
130130slightly less important than ` Panics ` , because failure is encoded into the type
131131system, but it's still a good thing to do.
132132
133- ```
133+ ``` rust
134134/// # Safety
135135# fn foo () {}
136136```
137137
138138If your function is ` unsafe ` , you should explain which invariants the caller is
139139responsible for upholding.
140140
141- ```
141+ ``` rust
142142/// # Examples
143143///
144144/// ```
@@ -154,7 +154,7 @@ method, and your users will love you for it. These examples go inside of
154154code block annotations, which we'll talk about in a moment, and can have
155155more than one section:
156156
157- ```
157+ ``` rust
158158/// # Examples
159159///
160160/// Simple `&str` patterns:
@@ -179,7 +179,7 @@ Let's discuss the details of these code blocks.
179179
180180To write some Rust code in a comment, use the triple graves:
181181
182- ```
182+ ``` rust
183183/// ```
184184/// println!("Hello, world");
185185/// ```
@@ -188,7 +188,7 @@ To write some Rust code in a comment, use the triple graves:
188188
189189If you want something that's not Rust code, you can add an annotation:
190190
191- ```
191+ ``` rust
192192/// ```c
193193/// printf("Hello, world\n");
194194/// ```
@@ -208,7 +208,7 @@ generate the documentation.
208208
209209Let's discuss our sample example documentation:
210210
211- ```
211+ ``` rust
212212/// ```
213213/// println!("Hello, world");
214214/// ```
@@ -219,7 +219,7 @@ You'll notice that you don't need a `fn main()` or anything here. `rustdoc` will
219219automatically add a main() wrapper around your code, and in the right place.
220220For example:
221221
222- ```
222+ ``` rust
223223/// ```
224224/// use std::rc::Rc;
225225///
@@ -230,7 +230,7 @@ For example:
230230
231231This will end up testing:
232232
233- ```
233+ ``` rust
234234fn main () {
235235 use std :: rc :: Rc ;
236236 let five = Rc :: new (5 );
@@ -259,7 +259,7 @@ with `///` we've been talking about? The raw text:
259259
260260looks different than the output:
261261
262- ```
262+ ``` rust
263263/// Some documentation.
264264# fn foo () {}
265265```
@@ -274,7 +274,7 @@ it makes the example more clear. You can use this technique to explain
274274longer examples in detail, while still preserving the testability of your
275275documentation. For example, this code:
276276
277- ```
277+ ``` rust
278278let x = 5 ;
279279let y = 6 ;
280280println! (" {}" , x + y );
@@ -284,23 +284,23 @@ Here's an explanation, rendered:
284284
285285First, we set ` x ` to five:
286286
287- ```
287+ ``` rust
288288let x = 5 ;
289289# let y = 6 ;
290290# println! (" {}" , x + y );
291291```
292292
293293Next, we set ` y ` to six:
294294
295- ```
295+ ``` rust
296296# let x = 5 ;
297297let y = 6 ;
298298# println! (" {}" , x + y );
299299```
300300
301301Finally, we print the sum of ` x ` and ` y ` :
302302
303- ```
303+ ``` rust
304304# let x = 5 ;
305305# let y = 6 ;
306306println! (" {}" , x + y );
@@ -340,7 +340,7 @@ explanation.
340340
341341Here’s an example of documenting a macro:
342342
343- ```
343+ ```rust
344344/// Panic with a given message unless an expression evaluates to true.
345345///
346346/// # Examples
@@ -388,7 +388,7 @@ but with a binary, there’s nothing to link to.
388388There are a few more annotations that are useful to help ` rustdoc ` do the right
389389thing when testing your code:
390390
391- ```
391+ ``` rust
392392/// ```ignore
393393/// fn foo() {
394394/// ```
@@ -400,7 +400,7 @@ what you want, as it's the most generic. Instead, consider annotating it
400400with ` text ` if it's not code, or using ` # ` s to get a working example that
401401only shows the part you care about.
402402
403- ```
403+ ``` rust
404404/// ```should_panic
405405/// assert!(false);
406406/// ```
@@ -410,7 +410,7 @@ only shows the part you care about.
410410` should_panic ` tells ` rustdoc ` that the code should compile correctly, but
411411not actually pass as a test.
412412
413- ```
413+ ``` rust
414414/// ```no_run
415415/// loop {
416416/// println!("Hello, world");
@@ -427,7 +427,7 @@ which you would want to make sure compile, but might run in an infinite loop!
427427
428428Rust has another kind of doc comment, ` //! ` . This comment doesn't document the next item, but the enclosing item. In other words:
429429
430- ```
430+ ``` rust
431431mod foo {
432432 // ! This is documentation for the `foo` module.
433433 // !
@@ -440,7 +440,7 @@ mod foo {
440440This is where you'll see ` //! ` used most often: for module documentation. If
441441you have a module in ` foo.rs ` , you'll often open its code and see this:
442442
443- ```
443+ ``` rust
444444// ! A module for using `foo`s.
445445// !
446446// ! 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.
461461When you write documentation in Markdown files, you don't need to prefix
462462the documentation with comments. For example:
463463
464- ```
464+ ``` rust
465465/// # Examples
466466///
467467/// ```
@@ -499,7 +499,7 @@ This `%` line needs to be the very first line of the file.
499499
500500At a deeper level, documentation comments are sugar for documentation attributes:
501501
502- ```
502+ ``` rust
503503/// this
504504# fn foo () {}
505505
@@ -509,7 +509,7 @@ At a deeper level, documentation comments are sugar for documentation attributes
509509
510510are the same, as are these:
511511
512- ```
512+ ``` rust
513513// ! this
514514
515515#![doc= " /// this" ]
@@ -546,7 +546,7 @@ pub use foo::bar;
546546You can control a few aspects of the HTML that ` rustdoc ` generates through the
547547` #![doc] ` version of the attribute:
548548
549- ```
549+ ``` rust
550550#![doc(html_logo_url = " http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png" ,
551551 html_favicon_url = " http://www.rust-lang.org/favicon.ico" ,
552552 html_root_url = " http://doc.rust-lang.org/" )];
0 commit comments