Issue cloning a String and then using it as a static lifetime &str

Having issues with below code. The commented line is the issue. If I try and clone the config object inline I get the error that temporary value dropped while borrowed, which I take it to mean that I have to set config.clone() into a variable and then use it. However if I create a variable like this: let cfg = config.clone(), and then use the variable on the commented line, cfg.host.as_str(), the compiler complains that the new variable does not live long enough. And so I need a &'static str to pass in but I don't see how I can do that.

pub async fn init_surreal_driver(config: &Config) -> Result<SurrealDriver, Box<dyn Error>> {
    let mut conn = SurrealWsConnection::new(
        config.clone().host.as_str(), // this line is the issue
        8000 as usize, 
        false
    );
    conn.connect().await?;

    let mut driver = SurrealDriver::new(conn);
    driver.sign_in(&config.surrealdb_user, &config.surrealdb_pass).await?;
    driver.use_ns_db(&config.surrealdb_ns, &config.surrealdb_ns).await?;

    Ok(driver)
}

What crate are those types coming from?

It's can also be helpful to include the compiler output when possible, since there tend to be important details in it that may not be obvious from a little piece of the code.

4 Likes

I need &'static str

You can't get &'static str from String.

If you give someone &'static str, you're telling them that they can hold onto that string forever and it will be valid until the program terminates.

You want to drop your String at some point, which will deallocate the buffer it owns, clearly making any &str referencing the String buffer invalid.

Normally, APIs will use Cow<'static, str> (or a variant thereof) instead of &'static str so that they can use either an owned String or an allocation-free literal &'static str.

If you absolutely must get &'static str from a runtime String, you can Box::leak(string.into_boxed_str()) — but note that this results in leaking the allocation such that it will never be deallocated. Doing this any more than a couple times at program startup likely isn't a good idea, as it will cause your program's memory usage to grow unboundedly as you deliberately promise to never deallocate the memory you're allocating.

3 Likes

When the compiler tells you you need a 'static lifetime, usually it's a misleading error. It's trying to say that all temporary references are forbidden in this context. The only correct solution is not to use &str then.

In your case it's hard to say why it happens without seeing definition of SurrealWsConnection::new and SurrealDriver types. If new actually requires &'static str that's a poor design choice on their part, it should be taking String or Cow<'static, str> or impl Into<Box<str>> or something like that which allows use of heap-allocated non-temporary strings.

If it's borrowing the argument, and giving you SurrealDriver<'temp>, then not cloning the config could help, because then the lifetime of the driver would be tied to lifetime of the Config outside of your function, outliving it.

2 Likes

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.