Search code examples
rustrust-obsolete

Why "explicit lifetime bound required" for Box<T> in struct?


Editor's note: This code no longer produces the same error after RFC 599 was implemented, but the concepts discussed in the answers are still valid.

I'm trying to compile this code:

trait A {
    fn f(&self);
}

struct S {
    a: Box<A>,
}

and I'm getting this error:

a.rs:6:13: 6:14 error: explicit lifetime bound required
a.rs:6     a: Box<A>,

I want S.a to own an instance of A, and don't see how that lifetime is appropriate here. What do I need to do to make the compiler happy?

My Rust version:

rustc --version
rustc 0.12.0-pre-nightly (79a5448f4 2014-09-13 20:36:02 +0000)

Solution

  • The problem here is that a trait can be implemented for references too, so if you don't specify the required lifetime for Box anything could be stored in there.

    You can see about lifetime requirements in this rfc.

    So one possible solution is to bind the lifetime so Send (we put I in S):

    trait A {
        fn f(&self);
    }
    
    struct I;
    
    impl A for I {
        fn f(&self) {
            println!("A for I")
        }
    }
    
    struct S {
        a: Box<A + Send>
    }
    
    fn main() {
        let s = S {
            a: box I
        };
        s.a.f();
    }
    

    The other is setting the lifetime to 'a (we can put a reference &I or I to S):

    trait A {
        fn f(&self);
    }
    
    struct I;
    
    impl A for I {
        fn f(&self) {
            println!("A for I")
        }
    }
    
    impl <'a> A for &'a I {
        fn f(&self) {
            println!("A for &I")
        }
    }
    
    struct S<'a> {
        a: Box<A + 'a>
    }
    
    fn main() {
        let s = S {
            a: box &I
        };
        s.a.f();
    }
    

    Note that this is more general and we can store both references and owned data (Send kind that has a lifetime of 'static) but you require a lifetime parameter everywhere the type is used.