reqwest::multipart::Part borrowing longer than it lives?

fn main() {

    struct Foo {
        foo: String,
    };

    struct Bar {
        bar: Vec<Foo>,
    }

    let bar = Bar{ bar: Vec::new() };

    for b in &bar.bar {
        let part = reqwest::multipart::Part::text("").file_name(&b.foo);
    }

}

And the error is:

error[E0597]: `bar.bar` does not live long enough
  --> src\main.rs:20:14
   |
20 |     for b in &bar.bar {
   |              ^^^^^^^^
   |              |
   |              borrowed value does not live long enough
   |              argument requires that `bar.bar` is borrowed for `'static`
...
57 | }
   | - `bar.bar` dropped here while still borrowed

What does it mean? Isn't everything dropped after the loop? Who is borrowing bar.bar?

The function only accepts owned types, but uses Cow<'static, str> to allow passing string literals too. This means that you can give it either an &'static str or an String.

Use b.foo.clone() to give it an owned variant.

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.