Skip to content

Commit 5a313e1

Browse files
authored
Merge pull request #93 from rust-lang-nursery/where
Where clauses
2 parents cb3cdaa + a6312c8 commit 5a313e1

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

guide/items.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,102 @@ Use `{}` for the full definition of the macro.
157157
macro_rules! foo {
158158
}
159159
```
160+
161+
### Generics
162+
163+
TODO
164+
165+
166+
### `where` clauses
167+
168+
These rules apply for `where` clauses on any item.
169+
170+
A `where` clause may immediately follow a closing bracket of any kind.
171+
Otherwise, it must start a new line, with no indent. Each component of a `where`
172+
clause must be on its own line and be block indented. There should be a trailing
173+
comma, unless the clause is terminated with a semicolon. If the `where` clause
174+
is followed by a block (or assignment), the block should be started on a new
175+
line. Examples:
176+
177+
```
178+
fn function<T, U>(args)
179+
where
180+
T: Bound,
181+
U: AnotherBound,
182+
{
183+
body
184+
}
185+
186+
fn foo<T>(
187+
args
188+
) -> ReturnType
189+
where
190+
T: Bound,
191+
{
192+
body
193+
}
194+
195+
fn foo<T, U>(
196+
args,
197+
) where
198+
T: Bound,
199+
U: AnotherBound,
200+
{
201+
body
202+
}
203+
204+
fn foo<T, U>(
205+
args
206+
) -> ReturnType
207+
where
208+
T: Bound,
209+
U: AnotherBound; // Note, no trailing comma.
210+
211+
type Foo<T>
212+
where
213+
T: Bound
214+
= Bar<T>;
215+
```
216+
217+
If a `where` clause is very short, we recommend using an inline bound on the
218+
type parameter.
219+
220+
221+
If a component of a `where` clause is long, it may be broken before `+` and
222+
further block indented. Each bound should go on its own line. E.g.,
223+
224+
```
225+
impl<T: ?Sized, Idx> IndexRanges<Idx> for T
226+
where
227+
T: Index<Range<Idx>, Output = Self::Output>
228+
+ Index<RangeTo<Idx>, Output = Self::Output>
229+
+ Index<RangeFrom<Idx>, Output = Self::Output>
230+
+ Index<RangeInclusive<Idx>, Output = Self::Output>
231+
+ Index<RangeToInclusive<Idx>, Output = Self::Output> + Index<RangeFull>
232+
```
233+
234+
#### Option - `where_single_line`
235+
236+
`where_single_line` is `false` by default. If `true`, then a where clause with
237+
exactly one component may be formatted on a single line if the rest of the
238+
item's signature is also kept on one line. In this case, there is no need for a
239+
trailing comma and if followed by a block, no need for a newline before the
240+
block. E.g.,
241+
242+
```
243+
// May be single-lined.
244+
fn foo<T>(args) -> ReturnType
245+
where T: Bound {
246+
body
247+
}
248+
249+
// Must be multi-lined.
250+
fn foo<T>(
251+
args
252+
) -> ReturnType
253+
where
254+
T: Bound,
255+
{
256+
body
257+
}
258+
```

0 commit comments

Comments
 (0)