# Syntactic sugar: Implement multiple trait for the same type at once

**URL:** https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154
**Category:** language design
**Created:** [February 16, 2022, 1:56pm UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154 "2022-02-16T13:56:42Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Qsh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsh/32/10418_2.png) [@Qsh](https://internals.rust-lang.org/u/Qsh)
#### Post date: [February 16, 2022, 1:56pm UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/1 "2022-02-16T13:56:42Z")

</div>

It's too painful when implementing features for generic constructs, I wish I didn't have to repeat the generic parameter list so many times.

```rust
trait A {
    fn a(&self);
}

trait B {
    fn b(&self);
}

struct T<V: Display> {
    v: V,
}

for<V> T<V> where V: Display {
    impl A {
        fn a(&self) {
            println!("{} - <T as A>::a", self.v);
        }
    }
    impl B {
        fn b(&self) {
            println!("{} - <T as B>::b", self.v);
        }
    }
}

```

I can only write it this way, and the editor document summary is ugly and long.

```rust
trait A {
    fn a(&self);
}

trait B {
    fn b(&self);
}

struct T<V: Display> {
    v: V,
}

impl<V> A for T<V> where V: Display {
    fn a(&self) {
        println!("{} - <T as A>::a", self.v);
    }
}

impl<V> B for T<V> where V: Display {
    fn b(&self) {
        println!("{} - <T as B>::b", self.v);
    }
}

```

---

<div class="post-metadata">

### Author: ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)
#### Post date: [February 16, 2022, 2:04pm UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/2 "2022-02-16T14:04:01Z")

</div>

See [RFC 2089: Implied Bounds](https://rust-lang.github.io/rfcs/2089-implied-bounds.html). Hopefully one day there will be no need to ever repeat bounds from the type declaration again. (Though, given there's been no movement on it in 5 years, and the stdlib has been _removing_ bounds from its types making it less useful, who knows if it will ever be implemented).

---

<div class="post-metadata">

### Author: ![Qsh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsh/32/10418_2.png) [@Qsh](https://internals.rust-lang.org/u/Qsh)
#### Post date: [February 16, 2022, 2:13pm UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/3 "2022-02-16T14:13:49Z")

</div>

Implicit bounds seem to have some strange side effects. All I wanted was a simple syntactic candy, because I had been writing this over and over again, especially when I changed the generic argument list for the structure, and it was too much work.

---

<div class="post-metadata">

### Author: ![Qsh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsh/32/10418_2.png) [@Qsh](https://internals.rust-lang.org/u/Qsh)
#### Post date: [February 16, 2022, 2:20pm UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/4 "2022-02-16T14:20:34Z")

</div>

![screenshot](https://us1.discourse-cdn.com/flex002/uploads/rustlang/original/2X/3/30260e44ef57100914e42d3ee37d78fa206c37e6.jpeg)

For this reason, the editor's outline of the 300-plus lines of code doesn't help me read the code very well anymore.

---

<div class="post-metadata">

### Author: ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)
#### Post date: [February 16, 2022, 2:50pm UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/5 "2022-02-16T14:50:17Z")

</div>

To be fair, that's also a question of editor UX. The outline should hide and/or abbreviate the generics and bounds stuff when the entry is collapsed. (Also, would be nice to better distinguish inherent impls from trait impls.)

---

<div class="post-metadata">

### Author: ![Qsh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsh/32/10418_2.png) [@Qsh](https://internals.rust-lang.org/u/Qsh)
#### Post date: [February 16, 2022, 3:14pm UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/6 "2022-02-16T15:14:12Z")

</div>

Even so, too many entries are displayed. How do you read plain text code without the help of an editor ? Of course, a part of the reason is the syntax of 'Impl Trait for Struct' to violate the logical subject (I mean the center of what we search for when we read the code). Such a sequence makes us unable to find the naked eye to find what we need.If we use the 'for Struct {}' syntax, we not only get a satisfactory editor outline, but it is also a great help for reading the code with the naked eye.

---

<div class="post-metadata">

### Author: ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)
#### Post date: [February 16, 2022, 6:29pm UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/7 "2022-02-16T18:29:44Z")

</div>

> [@Nemo157](#):
>
> and the stdlib has been _removing_ bounds from its types making it less useful

Note that this is part of the concern with the feature -- it would make removing bounds from a type a breaking change, and thus if it were accepted we could never do it again in `std`.

EDIT: Though admittedly most of the things being removed in `std` are on methods, not on types. (Like [https://doc.rust-lang.org/1.0.0/std/cell/struct.Cell.html](https://doc.rust-lang.org/1.0.0/std/cell/struct.Cell.html) has lots of bounds that are no longer there, but they're on the impl, not the struct itself.)

---

<div class="post-metadata">

### Author: ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)
#### Post date: [February 17, 2022, 5:50am UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/8 "2022-02-17T05:50:12Z")

</div>

I don't understand why the proposed syntax provides any improvement over the status quo. It's got an extra level of indentation for no reason, it's not even shorter, and the change is detrimental to readability because none of the `impl`s but the first one are close to the type name.

I just don't see the legitimate use case for this. If you are writing many (not just 2!) similar, short impls, that's a sign of missing abstraction, likely a supertrait. And if you are not willing to write 2 such impls, then you need to adjust your expectations.

---

<div class="post-metadata">

### Author: ![Qsh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsh/32/10418_2.png) [@Qsh](https://internals.rust-lang.org/u/Qsh)
#### Post date: [February 17, 2022, 6:56am UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/9 "2022-02-17T06:56:01Z")

</div>

My understanding of abstraction is exactly the opposite of yours: I believe that as long as functionality is complete, we should be inclined to break up a large super trait into independent trait, even though they tend to be implemented simultaneously on the same thing.

---

<div class="post-metadata">

### Author: ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)
#### Post date: [February 17, 2022, 7:13am UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/10 "2022-02-17T07:13:55Z")

</div>

I was not implying one should make giga-traits with all the methods in the world. I was suggesting to remove obvious repetition, nothing more.

---

<div class="post-metadata">

### Author: ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)
#### Post date: [May 18, 2022, 7:14am UTC](https://internals.rust-lang.org/t/syntactic-sugar-implement-multiple-trait-for-the-same-type-at-once/16154/11 "2022-05-18T07:14:47Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
