-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Amend style guide section for formatting where clauses in type aliases #114901
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -367,26 +367,49 @@ where | |
## Type aliases | ||
|
||
Keep type aliases on one line when they fit. If necessary to break the line, do | ||
so after the `=`, and block-indent the right-hand side: | ||
so before the `=`, and block-indent the right-hand side: | ||
|
||
```rust | ||
pub type Foo = Bar<T>; | ||
|
||
// If multi-line is required | ||
type VeryLongType<T, U: SomeBound> = | ||
AnEvenLongerType<T, U, Foo<T>>; | ||
type VeryLongType<T, U: SomeBound> | ||
= AnEvenLongerType<T, U, Foo<T>>; | ||
``` | ||
|
||
Where possible avoid `where` clauses and keep type constraints inline. Where | ||
that is not possible split the line before and after the `where` clause (and | ||
split the `where` clause as normal), e.g., | ||
that is not possible, prefer a trailing `where` clause over one that precedes | ||
|
||
the type. Split the line before and after a trailing `where` clause (and split | ||
the `where` clause as normal) and indent before the `=` and type, e.g., | ||
|
||
```rust | ||
type VeryLongType<T, U> | ||
= AnEvenLongerType<T, U, Foo<T>> | ||
where | ||
T: U::AnAssociatedType, | ||
U: SomeBound; | ||
``` | ||
|
||
When there is a `where` clause before the type, format it normally, and break | ||
after the last clause. Do not indent before the `=` to leave it visually | ||
distinct from the indented clauses. | ||
|
||
``` | ||
type WithPrecedingWC<T, U> | ||
where | ||
T: U::AnAssociatedType, | ||
U: SomeBound, | ||
= AnEvenLongerType<T, U, Foo<T>>; | ||
|
||
// Or with both a preceding and trailing where clause. | ||
type WithPrecedingWC<T, U> | ||
where | ||
T: U::AnAssociatedType, | ||
U: SomeBound, | ||
= AnEvenLongerType<T, U, Foo<T>> | ||
where | ||
T: U::AnAssociatedType2, | ||
U: SomeBound2; | ||
``` | ||
|
||
## Associated types | ||
|
Uh oh!
There was an error while loading. Please reload this page.