`format!` requires static lifetime?

Here is a minimal reproducible sample:

struct Hi<'a> {
    arr: Vec<Box<dyn Req + 'a>>
}

impl<'a> Hi<'a> {
    fn f(&self) {
        let x = format!("{:?}", self.arr);
        println!("{}", x);
    }
}

and here is the error:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/conn.rs:22:17
   |
22 |         let x = format!("{:?}", self.arr);
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the impl at 20:6...
  --> src/conn.rs:20:6
   |
20 | impl<'a> Hi<'a> {
   |      ^^
note: ...so that the types are compatible
  --> src/conn.rs:22:17
   |
22 |         let x = format!("{:?}", self.arr);
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected `(&std::vec::Vec<std::boxed::Box<dyn req::Req>>,)`
              found `(&std::vec::Vec<std::boxed::Box<(dyn req::Req + 'a)>>,)`
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
  --> src/conn.rs:22:33
   |
22 |         let x = format!("{:?}", self.arr);
   |                                 ^^^^^^^^
   = note: expected `std::fmt::Debug`
              found `std::fmt::Debug`
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

I am completely confused. IMHO format! should only borrow self.arr for a very short period of time (only borrow during its formatting process). However, it even requires self.arr to have static lifetime...

Thanks very much for any suggestions!

Can you share your reproducible sample via https://play.rust-lang.org ? I tried and it compiles well.

Thanks! Actually I made it wrong in Req. Here is what I found and solved: rust - `format!` requires static lifetime? - Stack Overflow

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.