Skip to content

Commit 167935d

Browse files
authored
Merge pull request #109 from rust-lang-nursery/items
Items
2 parents 4f6d71f + d8ebefb commit 167935d

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

guide/items.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ The proper ordering and spacing is:
2828

2929
Avoid comments within the signature itself.
3030

31+
If the function signature does not fit on one line, then break after the opening
32+
parenthesis and before the closing parenthesis and put each argument on its own
33+
block-indented line. For example,
34+
35+
```rust
36+
fn foo(
37+
arg1: i32,
38+
arg2: i32,
39+
) -> i32 {
40+
...
41+
}
42+
```
43+
44+
Note the trailing comma on the last argument.
45+
46+
3147
### Tuples and tuple structs
3248

3349
Write the type list as you would a parameter list to a function.
@@ -117,6 +133,7 @@ union Foo {
117133
}
118134
```
119135

136+
120137
### Tuple structs
121138

122139
Put the whole struct on one line if possible. Types in the parentheses should be
@@ -142,6 +159,45 @@ pub struct Foo(
142159
```
143160

144161

162+
### Traits
163+
164+
Trait items should be block-indented. If there are no items, the trait may be
165+
formatted on a single line. Otherwise there should be line-breaks after the
166+
opening brace and before the closing brace:
167+
168+
```rust
169+
trait Foo {}
170+
171+
pub trait Bar {
172+
...
173+
}
174+
```
175+
176+
If the trait has bounds, there should be a space after the colon but not before
177+
and before and after each `+`, e.g.,
178+
179+
```rust
180+
trait Foo: Debug + Bar {}
181+
```
182+
183+
Prefer not to line-break in the bounds if possible (consider using a `where`
184+
clause). Prefer to break between bounds than to break any individual bound. If
185+
you must break the bounds, put each bound (including the first) on its own
186+
block-indented line, break before the `+` and put the opening brace on its own
187+
line:
188+
189+
```rust
190+
pub trait IndexRanges:
191+
Index<Range<usize>, Output=Self>
192+
+ Index<RangeTo<usize>, Output=Self>
193+
+ Index<RangeFrom<usize>, Output=Self>
194+
+ Index<RangeFull, Output=Self>
195+
{
196+
...
197+
}
198+
```
199+
200+
145201
### Extern crate
146202

147203
`extern crate foo;`
@@ -171,9 +227,35 @@ macro_rules! foo {
171227
}
172228
```
173229

230+
174231
### Generics
175232

176-
TODO
233+
Prefer to put a generics clause on one line. Break other parts of an item
234+
declaration rather than line-breaking a generics clause. If a generics clause is
235+
large enough to require line-breaking, you should prefer to use a `where` clause
236+
instead.
237+
238+
Do not put spaces before or after `<` nor before `>`. Only put a space after `>`
239+
if it is followed by a word or opening brace, not an opening parenthesis. There
240+
should be a space after each comma and no trailing comma.
241+
242+
```rust
243+
fn foo<T: Display, U: Debug>(x: Vec<T>, y: Vec<U>) ...
244+
245+
impl<T: Display, U: Debug> SomeType<T, U> { ...
246+
```
247+
248+
If the generics clause must be formatted across multiple lines, each parameter
249+
should have its own block-indented line, there should be newlines after the
250+
opening bracket and before the closing bracket, and the should be a trailing
251+
comma.
252+
253+
```rust
254+
fn foo<
255+
T: Display,
256+
U: Debug,
257+
>(x: Vec<T>, y: Vec<U>) ...
258+
```
177259

178260
If an associated type is bound in a generic type, then there should be spaces on
179261
either side of the `=`:
@@ -182,6 +264,8 @@ either side of the `=`:
182264
<T: Example<Item = u32>>
183265
```
184266

267+
Prefer to use single-letter names for generic parameters.
268+
185269

186270
### `where` clauses
187271

0 commit comments

Comments
 (0)