1
+ ## Items
2
+
1
3
### Function definitions
2
4
3
5
In Rust, one finds functions by searching for ` fn [function-name] ` ; It's
@@ -30,10 +32,10 @@ let y = (11, 22, 33);
30
32
31
33
### Enums
32
34
33
- In the declaration, put each variant on its own line.
35
+ In the declaration, put each variant on its own line, block indented .
34
36
35
- Format each variant accordingly as either a ` struct ` , ` tuple struct ` , or ident ,
36
- which doesn't require special formatting.
37
+ Format each variant accordingly as either a struct, tuple struct, or identifier ,
38
+ which doesn't require special formatting (but without the ` struct ` keyword .
37
39
38
40
``` rust
39
41
enum FooBar {
@@ -46,6 +48,22 @@ enum FooBar {
46
48
}
47
49
```
48
50
51
+ If a struct variant is * short* (TODO link to definition), it may be formatted on
52
+ one line. In this case, do not use a trailing comma for the field list, but do
53
+ put spaces around braces:
54
+
55
+ ``` rust
56
+ enum FooBar {
57
+ Error { err : Box <Error >, line : u32 },
58
+ }
59
+ ```
60
+
61
+ In an enum with multiple struct variants, if any struct variant is written on
62
+ multiple lines, then the multi-line formatting should be used for all struct
63
+ variants. However, such a situation might be an indication that you should
64
+ factor out the fields of the variant into their own struct.
65
+
66
+
49
67
### Structs and Unions
50
68
51
69
Struct names follow on the same line as the ` struct ` keyword, with the opening
@@ -71,6 +89,10 @@ struct Foo {
71
89
}
72
90
```
73
91
92
+ Prefer using a unit struct to an empty struct (these only exist to simplify code
93
+ generation), but if you must use an empty struct, keep it on one line with no
94
+ space between the braces: ` struct Foo; ` or ` struct Foo {} ` .
95
+
74
96
The same guidelines are used for untagged union declarations.
75
97
76
98
``` rust
@@ -82,6 +104,31 @@ union Foo {
82
104
}
83
105
```
84
106
107
+ ### Tuple structs
108
+
109
+ Put the whole struct on one line if possible. Types in the parentheses should be
110
+ separated by a comma and space with no trailing comma. No spaces around the
111
+ parentheses or semi-colon:
112
+
113
+ ``` rust
114
+ pub struct Foo (String , u8 );
115
+ ```
116
+
117
+ Prefer unit structs to empty tuple structs (these only exist to simplify code
118
+ generation), e.g., ` struct Foo; ` rather than ` struct Foo(); ` .
119
+
120
+ For more than a few fields, prefer a proper struct with named fields. Given
121
+ this, a tuple struct should always fit on one line. If it does not, block format
122
+ the fields with a field on each line and a trailing comma:
123
+
124
+ ``` rust
125
+ pub struct Foo (
126
+ String ,
127
+ u8 ,
128
+ );
129
+ ```
130
+
131
+
85
132
### Extern crate
86
133
87
134
` extern crate foo; `
0 commit comments