Skip to content

Add trait bound styles #29137

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

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 28 additions & 0 deletions src/doc/style/features/traits/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,31 @@ To keep the cost of abstraction low, favor widely-known traits. Whenever
possible, implement and use traits provided as part of the standard library. Do
not introduce new traits for generics lightly; wait until there are a wide range
of types that can implement the type.

### Trait bound styles

For generics that has only **1-3** types with less than or equal to **1** trait bounds, trait
bounds could stay with the types.

```rust
fn test<A, B>(p1: A, p2: B) { ... }

fn test<A: Trait, B>(p1: A, p2: B) { ... }
```

You could always use the `where` clause for trait bounds.

```rust
fn test<A, B, C, D>(p1: A, p2: B, p3: C, p4: D) -> RetVal
where A: Trait1 + Trait2 + Send + 'static,
B: Trait2 + Send + Sync,
C: Trait3,
D: Trait4
{
// ...
}
```

In this situation, the left brace `{` should be on the different line, and the `where`
keyword should be in a new line and indented. The type parameters in the `where` clause
should align.