This document serves as a guide for editors and reviewers. Some conventions and content guidelines are specified in the introduction.
Use ATX-style headings (not Setext) with sentence case.
Do not use tabs, only spaces.
Files must end with a newline.
Lines must not end with spaces. Double spaces have semantic meaning, but can be invisible. Use a trailing backslash if you need a hard line break.
If possible, avoid double blank lines.
Do not use indented code blocks; use 3+ backticks code blocks instead.
Code blocks should have an explicit language tag.
Do not wrap long lines. This helps with reviewing diffs of the source.
Use smart punctuation instead of Unicode characters. For example, use ---
for em-dash instead of the Unicode character. Characters like em-dash can be difficult to see in a fixed-width editor, and some editors may not have easy methods to enter such characters.
Links should be relative with the .md
extension. Links to other rust-lang books that are published with the reference should also be relative so that the linkchecker can validate them.
Links to the standard library should use rustdoc-style links described in Standard library links.
The use of reference links is preferred, with shortcuts if appropriate. Place the sorted link reference definitions at the bottom of the file, or at the bottom of a section if there are an unusually large number of links that are specific to the section.
Example of shortcut link: [enumerations] Example of reference link with label: [block expression][block] [block]: expressions/block-expr.md [enumerations]: types/enum.md
See the Conventions section for formatting callouts such as notes, edition differences, and warnings.
There are automated checks for some of these rules. Run cargo run --manifest-path style-check/Cargo.toml -- src
to run them locally.
Code examples should use code blocks with triple backticks. The language should always be specified (such as rust
).
println!("Hello!");
See https://rust-lang.github.io/mdBook/format/theme/syntax-highlighting.html#supported-languages for a list of supported languages.
Rust examples are tested via rustdoc, and should include the appropriate annotations:
edition2015
or edition2018
--- If it is edition-specific (see book.toml
for the default).no_run
--- The example should compile successfully, but should not be executed.should_panic
--- The example should compile and run, but produce a panic.compile_fail
--- The example is expected to fail to compile.ignore
--- The example shouldn't be built or tested. This should be avoided if possible. Usually this is only necessary when the testing framework does not support it (such as external crates or modules, or a proc-macro), or it contains pseudo-code which is not valid Rust. An HTML comment such as <!-- ignore: requires extern crate -->
should be placed before the example to explain why it is ignored.Exxxx
--- If the example is expected to fail to compile with a specific error code, include that code so that rustdoc will check that the expected code is used.See the rustdoc documentation for more detail.
The following are extensions provided by mdbook-spec
.
Most clauses should be preceded with a rule. Rules can be specified in the markdown source with the following on a line by itself:
r[foo.bar]
The rule name should be lowercase, with periods separating from most general to most specific (like r[array.repeat.zero]
).
Rules can be linked to by their ID using markdown such as [foo.bar]
. There are automatic link references so that any rule can be referred to from any page in the book.
In the HTML, the rules are clickable just like headers.
When assigning rules to new paragraphs, or when modifying rule names, use the following guidelines:
[!NOTE]
or more specific admonitions in the future.intro
: The beginning paragraph of each section - should explain the construct being defined overall.syntax
: Syntax definitions or explanations when BNF syntax definitions are not used.namespace
: For items only, specifies the namespace(s) the item introduces a name in. May also be used elsewhere when defining a namespace (e.g. r[attribute.diagnostic.namespace]
)._
s with -
s)._
s in the name, use -
characters to separate words within a “subrule”.x86
rather than separately listing i586
, i686
and x86_64
, and if a rule applies to all ELF platforms, it should be named elf
rather than listing every ELF OS).You should link to the standard library without specifying a URL in a fashion similar to rustdoc intra-doc links. Some examples:
We can link to the page on Option
:
[`std::option::Option`]
In these links, generics are ignored and can be included:
[`std::option::Option<T>`]
If we don't want the full path in the text, we can write:
[`Option`](std::option::Option)
Macros can end in !
. This can be helpful for disambiguation. For example, this refers to the macro rather than the module:
[`alloc::vec!`]
Explicit namespace disambiguation is also supported:
[`std::vec`](mod@std::vec)
Beware there are some limitations, for example:
std_arch
don't work due to https://github.com/rust-lang/rust/issues/96506.When running into a rustdoc limitation, consider manually linking to the correct page using a relative link. For example, ../std/arch/macro.is_x86_feature_detected.html
.
Admonitions use a style similar to GitHub-flavored markdown, where the style name is placed at the beginning of a blockquote, such as:
> [!WARNING] > This is a warning.
All this does is apply a CSS class to the blockquote. You should define the color or style of the rule in the css/custom.css
file if it isn't already defined.
Idioms and styling to avoid:
The following are guidelines for the content of the reference.
The reference does not document which targets exist, or the properties of specific targets. The reference may refer to platforms or target properties where required by the language. Some examples:
target_os
are specified to exist, but not what their values must be.windows_subsystem
attribute specifies that it only works on Windows platforms.target_feature
attribute specify the architectures that are supported.The main text and flow should document only the current edition. Whenever there is a difference between editions, the differences should be called out with an “Edition differences” block.