]> git.proxmox.com Git - rustc.git/blame - src/doc/style/style/whitespace.md
New upstream version 1.12.1+dfsg1
[rustc.git] / src / doc / style / style / whitespace.md
CommitLineData
85aaf69f
SL
1% Whitespace [FIXME: needs RFC]
2
3* Lines must not exceed 99 characters.
4* Use 4 spaces for indentation, _not_ tabs.
5* No trailing whitespace at the end of lines or files.
6
7### Spaces
8
9* Use spaces around binary operators, including the equals sign in attributes:
10
a7813a04 11```rust,ignore
85aaf69f 12#[deprecated = "Use `bar` instead."]
62682a34 13fn foo(a: usize, b: usize) -> usize {
85aaf69f
SL
14 a + b
15}
16```
17
18* Use a space after colons and commas:
19
a7813a04 20```rust,ignore
85aaf69f
SL
21fn foo(a: Bar);
22
23MyStruct { foo: 3, bar: 4 }
24
25foo(bar, baz);
26```
27
28* Use a space after the opening and before the closing brace for
29 single line blocks or `struct` expressions:
30
a7813a04 31```rust,ignore
85aaf69f
SL
32spawn(proc() { do_something(); })
33
34Point { x: 0.1, y: 0.3 }
35```
36
37### Line wrapping
38
39* For multiline function signatures, each new line should align with the
40 first parameter. Multiple parameters per line are permitted:
41
a7813a04 42```rust,ignore
85aaf69f
SL
43fn frobnicate(a: Bar, b: Bar,
44 c: Bar, d: Bar)
45 -> Bar {
46 ...
47}
48
49fn foo<T: This,
50 U: That>(
51 a: Bar,
52 b: Bar)
53 -> Baz {
54 ...
55}
56```
57
58* Multiline function invocations generally follow the same rule as for
59 signatures. However, if the final argument begins a new block, the
60 contents of the block may begin on a new line, indented one level:
61
a7813a04 62```rust,ignore
85aaf69f
SL
63fn foo_bar(a: Bar, b: Bar,
64 c: |Bar|) -> Bar {
65 ...
66}
67
68// Same line is fine:
69foo_bar(x, y, |z| { z.transpose(y) });
70
71// Indented body on new line is also fine:
72foo_bar(x, y, |z| {
73 z.quux();
74 z.rotate(x)
75})
76```
77
78> **[FIXME]** Do we also want to allow the following?
79>
a7813a04 80> ```rust,ignore
85aaf69f
SL
81> frobnicate(
82> arg1,
83> arg2,
84> arg3)
85> ```
86>
87> This style could ease the conflict between line length and functions
88> with many parameters (or long method chains).
89
90### Matches
91
92> * **[Deprecated]** If you have multiple patterns in a single `match`
93> arm, write each pattern on a separate line:
94>
a7813a04 95> ```rust,ignore
85aaf69f
SL
96> match foo {
97> bar(_)
98> | baz => quux,
99> x
100> | y
101> | z => {
102> quuux
103> }
104> }
105> ```
106
107### Alignment
108
109Idiomatic code should not use extra whitespace in the middle of a line
110to provide alignment.
111
112
a7813a04 113```rust,ignore
85aaf69f
SL
114// Good
115struct Foo {
116 short: f64,
117 really_long: f64,
118}
119
120// Bad
121struct Bar {
122 short: f64,
123 really_long: f64,
124}
125
126// Good
127let a = 0;
128let radius = 7;
129
130// Bad
131let b = 0;
132let diameter = 7;
133```