1+ # Coding conventions
2+
13This file offers some tips on the coding conventions for rustc. This
24chapter covers [ formatting] ( #formatting ) , [ coding for correctness] ( #cc ) ,
35[ using crates from crates.io] ( #cio ) , and some tips on
46[ structuring your PR for easy review] ( #er ) .
57
68<a id =" formatting " ></a >
79
8- # Formatting and the tidy script
10+ ## Formatting and the tidy script
911
1012rustc is moving towards the [ Rust standard coding style] [ fmt ] .
1113
@@ -28,7 +30,7 @@ pass the <!-- date-check: nov 2022 --> `--edition=2021` argument yourself when c
2830
2931[ `rustfmt` ] :https://github.com/rust-lang/rustfmt
3032
31- ## Formatting C++ code
33+ ### Formatting C++ code
3234
3335The compiler contains some C++ code for interfacing with parts of LLVM that
3436don't have a stable C API.
@@ -41,7 +43,7 @@ When modifying that code, use this command to format it:
4143This uses a pinned version of ` clang-format ` , to avoid relying on the local
4244environment.
4345
44- ## Formatting and linting Python code
46+ ### Formatting and linting Python code
4547
4648The Rust repository contains quite a lot of Python code. We try to keep
4749it both linted and formatted by the [ ruff] [ ruff ] tool.
@@ -65,7 +67,7 @@ environment.
6567
6668<!-- REUSE-IgnoreStart -->
6769<!-- Prevent REUSE from interpreting the heading as a copyright notice -->
68- ## Copyright notice
70+ ### Copyright notice
6971<!-- REUSE-IgnoreEnd -->
7072
7173In the past, files began with a copyright and license notice. Please ** omit**
@@ -75,7 +77,7 @@ MIT/Apache-2.0).
7577All of the copyright notices should be gone by now, but if you come across one
7678in the rust-lang/rust repo, feel free to open a PR to remove it.
7779
78- ## Line length
80+ ### Line length
7981
8082Lines should be at most 100 characters. It's even better if you can
8183keep things to 80.
@@ -88,18 +90,18 @@ that case, you can add a comment towards the top of the file like so:
8890// ignore-tidy-linelength
8991```
9092
91- ## Tabs vs spaces
93+ ### Tabs vs spaces
9294
9395Prefer 4-space indent.
9496
9597<a id =" cc " ></a >
9698
97- # Coding for correctness
99+ ## Coding for correctness
98100
99101Beyond formatting, there are a few other tips that are worth
100102following.
101103
102- ## Prefer exhaustive matches
104+ ### Prefer exhaustive matches
103105
104106Using ` _ ` in a match is convenient, but it means that when new
105107variants are added to the enum, they may not get handled correctly.
@@ -109,7 +111,7 @@ other treatment? Unless the answer is "low", then prefer an
109111exhaustive match. (The same advice applies to ` if let ` and `while
110112let`, which are effectively tests for a single variant.)
111113
112- ## Use "TODO" comments for things you don't want to forget
114+ ### Use "TODO" comments for things you don't want to forget
113115
114116As a useful tool to yourself, you can insert a ` // TODO ` comment
115117for something that you want to get back to before you land your PR:
@@ -136,13 +138,13 @@ if foo {
136138
137139<a id =" cio " ></a >
138140
139- # Using crates from crates.io
141+ ## Using crates from crates.io
140142
141143See the [ crates.io dependencies] [ crates ] section.
142144
143145<a id =" er " ></a >
144146
145- # How to structure your PR
147+ ## How to structure your PR
146148
147149How you prepare the commits in your PR can make a big difference for the
148150reviewer. Here are some tips.
@@ -172,7 +174,7 @@ require that every intermediate commit successfully builds – we only
172174expect to be able to bisect at a PR level. However, if you * can* make
173175individual commits build, that is always helpful.
174176
175- # Naming conventions
177+ ## Naming conventions
176178
177179Apart from normal Rust style/naming conventions, there are also some specific
178180to the compiler.
0 commit comments