]> git.lizzy.rs Git - rust.git/blobdiff - src/doc/rustdoc.md
auto merge of #17432 : nick29581/rust/contrib, r=brson
[rust.git] / src / doc / rustdoc.md
index 1034c776ea6dd59137c305e66e84bc79f9e064fa..07a93e58456b94a680378e960193788db994c7b7 100644 (file)
@@ -51,7 +51,7 @@ Calculates the factorial of a number.
 
 Given the input integer `n`, this function will calculate `n!` and return it.
 "]
-pub fn factorial(n: int) -> int { if n < 2 {1} else {n * factorial(n)} }
+pub fn factorial(n: int) -> int { if n < 2 {1} else {n * factorial(n - 1)} }
 # fn main() {}
 ~~~
 
@@ -103,6 +103,17 @@ rustdoc can also generate JSON, for consumption by other tools, with
 `rustdoc --output-format json`, and also consume already-generated JSON with
 `rustdoc --input-format json`.
 
+rustdoc also supports personalizing the output from crates' documentation,
+similar to markdown options.
+
+- `--html-in-header FILE`: includes the contents of `FILE` at the
+  end of the `<head>...</head>` section.
+- `--html-before-content FILE`: includes the contents of `FILE`
+  directly after `<body>`, before the rendered content (including the
+  search bar).
+- `--html-after-content FILE`: includes the contents of `FILE`
+  after all the rendered content.
+
 # Using the Documentation
 
 The web pages generated by rustdoc present the same logical hierarchy that one
@@ -122,33 +133,41 @@ source code.
 
 To test documentation, the `--test` argument is passed to rustdoc:
 
-~~~ {.notrust}
+~~~ {.sh}
 rustdoc --test crate.rs
 ~~~
 
 ## Defining tests
 
 Rust documentation currently uses the markdown format, and rustdoc treats all
-code blocks as testable-by-default. In order to not run a test over a block of
-code, the `ignore` string can be added to the three-backtick form of markdown
-code block.
+code blocks as testable-by-default unless they carry a language tag of another
+language. In order to not run a test over a block of code, the `ignore` string
+can be added to the three-backtick form of markdown code block.
 
-~~~notrust
+~~~md
 ```
 // This is a testable code block
 ```
 
+```rust{.example}
+// This is rust and also testable
+```
+
 ```ignore
 // This is not a testable code block
 ```
 
     // This is a testable code block (4-space indent)
+
+```sh
+# this is shell code and not tested
+```
 ~~~
 
 You can specify that the test's execution should fail with the `should_fail`
 directive.
 
-~~~notrust
+~~~md
 ```should_fail
 // This code block is expected to generate a failure when run
 ```
@@ -157,19 +176,31 @@ directive.
 You can specify that the code block should be compiled but not run with the
 `no_run` directive.
 
-~~~notrust
+~~~md
 ```no_run
 // This code will be compiled but not executed
 ```
 ~~~
 
+Lastly, you can specify that a code block be compiled as if `--test`
+were passed to the compiler using the `test_harness` directive.
+
+~~~md
+```test_harness
+#[test]
+fn foo() {
+    fail!("oops! (will run & register as failure)")
+}
+```
+~~~
+
 Rustdoc also supplies some extra sugar for helping with some tedious
 documentation examples. If a line is prefixed with `# `, then the line
 will not show up in the HTML documentation, but it will be used when
 testing the code block (NB. the space after the `#` is required, so
 that one can still write things like `#[deriving(Eq)]`).
 
-~~~notrust
+~~~md
 ```
 # /!\ The three following lines are comments, which are usually stripped off by
 # the doc-generating tool.  In order to display them anyway in this particular
@@ -194,7 +225,7 @@ uses is build on crate `test`, which is also used when you compile crates with
 rustc's `--test` flag. Extra arguments can be passed to rustdoc's test harness
 with the `--test-args` flag.
 
-~~~ {.notrust}
+~~~console
 # Only run tests containing 'foo' in their name
 $ rustdoc --test lib.rs --test-args 'foo'
 
@@ -218,16 +249,16 @@ detected by a `.md` or `.markdown` extension.
 There are 4 options to modify the output that Rustdoc creates.
 
 - `--markdown-css PATH`: adds a `<link rel="stylesheet">` tag pointing to `PATH`.
-- `--markdown-in-header FILE`: includes the contents of `FILE` at the
+- `--html-in-header FILE`: includes the contents of `FILE` at the
   end of the `<head>...</head>` section.
-- `--markdown-before-content FILE`: includes the contents of `FILE`
+- `--html-before-content FILE`: includes the contents of `FILE`
   directly after `<body>`, before the rendered content (including the
   title).
-- `--markdown-after-content FILE`: includes the contents of `FILE`
+- `--html-after-content FILE`: includes the contents of `FILE`
   directly before `</body>`, after all the rendered content.
 
 All of these can be specified multiple times, and they are output in
-the order in which they are specified. The first line of the file must
+the order in which they are specified. The first line of the file being rendered must
 be the title, prefixed with `%` (e.g. this page has `% Rust
 Documentation` on the first line).