2
pub struct Table<T> {
    table: Vec<Vec<T>>
}

impl<i32> Display for Table<i32>
    where i32: Display
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (index, line) in self.table.iter().enumerate() {
            write!(f, "{}:\t", index+1);

            let mut sum = 0i32;
            for task in line {
                write!(f, "{} ", task);
                sum += *task;
            }

            write!(f, " : {}\n", sum);
        }

        Result::Ok(())
    }
}

This code results in a confusing error message:

error[E0308]: mismatched types
  --> src/table.rs:48:24
   |
48 |                 sum += *task;
   |                        ^^^^^ expected i32, found type parameter
   |
   = note: expected type `i32` (i32)
   = note:    found type `i32` (type parameter)

And I don't know what to do.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
dakue
  • 38
  • 6

1 Answers1

4

You have accidentally introduced a type parameter called i32 (instead of, usually, T). I agree with you here, that the compiler diagnostic could be better.

Adding something after impl in angle brackets, likes this:

impl<i32> Display for Table<i32>

... will always introduce a new type parameter, just like e.g. impl<T>. To fix your problem, just remove this type parameter declaration from the impl keyword:

impl Display for Table<i32> { ... }

Note that I also removed the where bound which doesn't make sense anymore. You can't apply bounds to concrete types.


Related:

Community
  • 1
  • 1
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305