Skip to content

Commit

Permalink
Where clauses
Browse files Browse the repository at this point in the history
Closes #38
Closes #74
Closes #75
cc #89
  • Loading branch information
nrc committed Aug 31, 2017
1 parent 6c702c4 commit a6312c8
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions guide/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,102 @@ Use `{}` for the full definition of the macro.
macro_rules! foo {
}
```

### Generics

TODO


### `where` clauses

These rules apply for `where` clauses on any item.

A `where` clause may immediately follow a closing bracket of any kind.
Otherwise, it must start a new line, with no indent. Each component of a `where`
clause must be on its own line and be block indented. There should be a trailing
comma, unless the clause is terminated with a semicolon. If the `where` clause
is followed by a block (or assignment), the block should be started on a new
line. Examples:

```
fn function<T, U>(args)
where
T: Bound,
U: AnotherBound,
{
body
}
fn foo<T>(
args
) -> ReturnType
where
T: Bound,
{
body
}
fn foo<T, U>(
args,
) where
T: Bound,
U: AnotherBound,
{
body
}
fn foo<T, U>(
args
) -> ReturnType
where
T: Bound,
U: AnotherBound; // Note, no trailing comma.
type Foo<T>
where
T: Bound
= Bar<T>;
```

If a `where` clause is very short, we recommend using an inline bound on the
type parameter.


If a component of a `where` clause is long, it may be broken before `+` and
further block indented. Each bound should go on its own line. E.g.,

```
impl<T: ?Sized, Idx> IndexRanges<Idx> for T
where
T: Index<Range<Idx>, Output = Self::Output>
+ Index<RangeTo<Idx>, Output = Self::Output>
+ Index<RangeFrom<Idx>, Output = Self::Output>
+ Index<RangeInclusive<Idx>, Output = Self::Output>
+ Index<RangeToInclusive<Idx>, Output = Self::Output> + Index<RangeFull>
```

#### Option - `where_single_line`

`where_single_line` is `false` by default. If `true`, then a where clause with
exactly one component may be formatted on a single line if the rest of the
item's signature is also kept on one line. In this case, there is no need for a
trailing comma and if followed by a block, no need for a newline before the
block. E.g.,

```
// May be single-lined.
fn foo<T>(args) -> ReturnType
where T: Bound {
body
}
// Must be multi-lined.
fn foo<T>(
args
) -> ReturnType
where
T: Bound,
{
body
}
```

0 comments on commit a6312c8

Please sign in to comment.