2

I'm getting this compiler error, that I don't I understand nor know how to fix:

error[E0277]: the trait bound `&'a mut Runnable + 'a: Runnable` is not satisfied
  --> src/main.rs:11:9
   |
11 |         &mut self.runnables[id]
   |         ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Runnable` is not implemented for `&'a mut Runnable + 'a`
   |
   = note: required for the cast to the object type `Runnable + 'a`

I've simplified my code down to this:

pub trait Runnable {
    fn name(&self) -> &str;
}

pub struct RunList<'a> {
    runnables: Vec<&'a mut Runnable>,
}

impl<'a> RunList<'a> {
    pub fn get(&self, id: usize) -> &'a mut Runnable {
        &mut self.runnables[id]
    }
}

fn main() {}

Playground

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Andrew Mackenzie
  • 5,477
  • 5
  • 48
  • 70
  • @Shepmaster Why is this a duplicate of the other question? – nbro Mar 31 '18 at 13:38
  • @nbro because it explains how to implement a trait for references to a trait. Do you believe that to be in error? – Shepmaster Mar 31 '18 at 13:39
  • @Shepmaster I was just asking because I was not understanding (given that my knowledge of Rust is still reduced). You can delete these comments afterwards. – nbro Mar 31 '18 at 13:43
  • @nbro It's *always* possible that I've closed something as a duplicate in error, so it's always worth double-checking. Testing out the proposed solution has led to some extra errors; I'm seeing if there's something fundamentally wrong with it. – Shepmaster Mar 31 '18 at 13:46
  • 1
    @Shepmaster OP posted a [question](https://stackoverflow.com/q/49587155/7274990) (which is most likely also a duplicate) very closely related to this one. I think he really only wants to return an element from the runnables vec. I can imagine `&mut self.runnables[id]` is just an attempt to solve the problem in the original question. – Calculator Mar 31 '18 at 14:00
  • @Calculator ah, yes, perfect. OP **did the right thing** by asking to separate questions. That means that this error can be answered here and the original problem can be answered there. Nice! – Shepmaster Mar 31 '18 at 14:03

0 Answers0