I don't understand this `borrowed value does not live long enough`

Wish everyone a happy new year :stuck_out_tongue:

I don't quite follow why this case is not compiling. link

  --> src/cmd/clone.rs:28:39
27 |         let body = res.bytes()?;
   |             ---- binding `body` declared here
28 |         let _ = parse_smart_response(&body[..])?;
   |                 ----------------------^^^^-----
   |                 |                     |
   |                 |                     borrowed value does not live long enough
   |                 argument requires that `body` is borrowed for `'static`
29 |     };
   |     - `body` dropped here while still borrowed

I don't understand where does this 'static lifetime come from. This looks to me should be working. Can someone explain? Thanks


The 'static is likely due to the use of question mark operator in parse_smart_response(&body[..])?.

The error type is nom::Err<nom::error::Error<&'a [u8]>> (from this type definition). The ? is used within a function with anyhow::Error error type. The relevant From conversion here is what requires nom::Err<nom::error::Error<&'a [u8]>>: 'static and thus 'a == 'static.


This, which compiles, will help you see it:

let v = match parse_smart_response(&body[..]) {
    Ok((_, v)) => v,
    Err(_) => {
1 Like

Thanks for the hint. You are right! Thanks. I was scratching my head to understand why this happens.

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.