Skip to content

Using format_args!() to build a fmt::Arguments fails to compile #51905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
agrover opened this issue Jun 29, 2018 · 2 comments
Closed

Using format_args!() to build a fmt::Arguments fails to compile #51905

agrover opened this issue Jun 29, 2018 · 2 comments

Comments

@agrover
Copy link

agrover commented Jun 29, 2018

This code:

fn main() {
    let test = &"hello".to_owned();
    let args = format_args!("{}", test);
}

returns this error:

6 |     let args = format_args!("{}", test);
  |                             ^^^^       - temporary value dropped here while still borrowed
  |                             |
  |                             temporary value does not live long enough
7 | }
  | - temporary value needs to live until here
  |
  = note: consider using a `let` binding to increase its lifetime

This makes no sense to me. Doesn't "{}" have a static lifetime?

btw this is related to rust-lang/log#282, I'm trying to recreate a fmt::Arguments to build a log::Record. This with 1.27.0.

Thanks!

@sfackler
Copy link
Member

The error message pointing to the "{}" is a bit misleading - the temporary value is actually in the code generated by format_args, and the span happens to point to the format string.

Your example expands to this:

fn main() {
    let test = &"hello".to_owned();
    let args = ::std::fmt::Arguments::new_v1_formatted(
        &[""],
        &match (&test,) {
            (arg0,) => [::std::fmt::ArgumentV1::new(arg0, ::std::fmt::Display::fmt)],
        },
        &[::std::fmt::rt::v1::Argument {
            position: ::std::fmt::rt::v1::Position::At(0usize),
            format: ::std::fmt::rt::v1::FormatSpec {
                fill: ' ',
                align: ::std::fmt::rt::v1::Alignment::Unknown,
                flags: 0u32,
                precision: ::std::fmt::rt::v1::Count::Implied,
                width: ::std::fmt::rt::v1::Count::Implied,
            },
        }],
    );
}

Building that directly points to the actual code that's the problem:

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:7:10
   |
7  |           &match (&test,) {
   |  __________^
8  | |             (arg0,) => [::std::fmt::ArgumentV1::new(arg0, ::std::fmt::Display::fmt)],
9  | |         },
   | |_________^ temporary value does not live long enough
...
20 |       );
   |        - temporary value dropped here while still borrowed
21 |   }
   |   - temporary value needs to live until here
   |
   = note: consider using a `let` binding to increase its lifetime

@agrover
Copy link
Author

agrover commented Jun 29, 2018

Thanks for your help! Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants