Skip to content

Commit 5780c31

Browse files
committed
enums, variants, tuple structs
closes #31 cc #89
1 parent 6c702c4 commit 5780c31

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

guide/items.md

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Items
2+
13
### Function definitions
24

35
In Rust, one finds functions by searching for `fn [function-name]`; It's
@@ -30,10 +32,10 @@ let y = (11, 22, 33);
3032

3133
### Enums
3234

33-
In the declaration, put each variant on its own line.
35+
In the declaration, put each variant on its own line, block indented.
3436

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.
3739

3840
```rust
3941
enum FooBar {
@@ -46,6 +48,22 @@ enum FooBar {
4648
}
4749
```
4850

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+
4967
### Structs and Unions
5068

5169
Struct names follow on the same line as the `struct` keyword, with the opening
@@ -71,6 +89,10 @@ struct Foo {
7189
}
7290
```
7391

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+
7496
The same guidelines are used for untagged union declarations.
7597

7698
```rust
@@ -82,6 +104,31 @@ union Foo {
82104
}
83105
```
84106

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+
85132
### Extern crate
86133

87134
`extern crate foo;`

0 commit comments

Comments
 (0)