]> git.lizzy.rs Git - rust.git/blob - src/doc/style/style/whitespace.md
Changed issue number to 36105
[rust.git] / src / doc / style / style / whitespace.md
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
11 ```rust,ignore
12 #[deprecated = "Use `bar` instead."]
13 fn foo(a: usize, b: usize) -> usize {
14     a + b
15 }
16 ```
17
18 * Use a space after colons and commas:
19
20 ```rust,ignore
21 fn foo(a: Bar);
22
23 MyStruct { foo: 3, bar: 4 }
24
25 foo(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
31 ```rust,ignore
32 spawn(proc() { do_something(); })
33
34 Point { 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
42 ```rust,ignore
43 fn frobnicate(a: Bar, b: Bar,
44               c: Bar, d: Bar)
45               -> Bar {
46     ...
47 }
48
49 fn 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
62 ```rust,ignore
63 fn foo_bar(a: Bar, b: Bar,
64            c: |Bar|) -> Bar {
65     ...
66 }
67
68 // Same line is fine:
69 foo_bar(x, y, |z| { z.transpose(y) });
70
71 // Indented body on new line is also fine:
72 foo_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 >
80 > ```rust,ignore
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 >
95 >     ```rust,ignore
96 >     match foo {
97 >         bar(_)
98 >         | baz => quux,
99 >         x
100 >         | y
101 >         | z => {
102 >             quuux
103 >         }
104 >     }
105 >     ```
106
107 ### Alignment
108
109 Idiomatic code should not use extra whitespace in the middle of a line
110 to provide alignment.
111
112
113 ```rust,ignore
114 // Good
115 struct Foo {
116     short: f64,
117     really_long: f64,
118 }
119
120 // Bad
121 struct Bar {
122     short:       f64,
123     really_long: f64,
124 }
125
126 // Good
127 let a = 0;
128 let radius = 7;
129
130 // Bad
131 let b        = 0;
132 let diameter = 7;
133 ```