Skip to content

Where clauses #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
```