3

While learning Rust, I stumbled upon the following scenario.

Consider this example:

use std::fmt::Debug;

struct Foo<T>
where
    T: Debug,
{
    data: T,
}

impl<T> Drop for Foo<T> {
    fn drop(&mut self) {
        println!("Dropping Foo with data: '{:?}'!", self.data);
    }
}

This does not compile:

error[E0277]: `T` doesn't implement `std::fmt::Debug`
  --> src/lib.rs:10:9
   |
10 | impl<T> Drop for Foo<T> {
   |         ^^^^ `T` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
   |
   = help: the trait `std::fmt::Debug` is not implemented for `T`
   = help: consider adding a `where T: std::fmt::Debug` bound
note: required by `Foo`
  --> src/lib.rs:3:1
   |
3  | / struct Foo<T>
4  | | where
5  | |     T: Debug,
6  | | {
7  | |     data: T,
8  | | }
   | |_^

The way to fix this is to explicitly define the type constraint when implementing Drop:

impl<T> Drop for Foo<T>
where
    T: Debug,
{
    fn drop(&mut self) {
        println!("Dropping Foo with data: '{:?}'!", self.data);
    }
}

However, I end up with the same constraint on multiple places, which does not make much sense to me.

Defining the constraint at struct declaration level should be enough, and the compiler should assume that T is always going to implement Debug, because there's simply no way that we can initialize Foo without a T: Debug

Why is this explicitness required? Is this intentional or is this a current compiler limitation?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • See [Why is the bound `T: 'a` required in order to store a reference `&'a T`?](https://stackoverflow.com/q/41794800/155423) for a similar case that was streamlined. – Shepmaster Jul 30 '19 at 01:27

1 Answers1

2

There's the "Implied bounds" RFC 2089, which - when implemented - will make the duplication unnecessary.

Specifically for the T: Debug case I wouldn't have that bound in the struct definition.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Nickolay
  • 31,095
  • 13
  • 107
  • 185
  • When you find an answer, please [search for existing answers](https://www.google.com/search?q=site:stackoverflow.com%20rust%20RFC%202089) that match. This is the single best way to identify duplicates and avoid repeating information across many SO Q&A. – Shepmaster Jul 30 '19 at 01:22