How can I pass a non-static string slice to a function with a parameter Into<Cow<'static, str>>?

I'm specifically looking at this one:

So far I didn't manage to pass something as value that is not static, which makes the struct implementation relatively useless to me. Of course there has to be a way to pass dynamically created strings to this method, right?

You would at least want to have dynamic cookie values, wouldn't I?

I bet I'm missing something completely obvious.

You can allocate


This works because String: 'static. You can also pass in string literals, but you wanted dynamically created string. So you must allocate a new string.

Okay, to be more specific, I want to use the crate like this:

pub struct CookieFactory {
    domain: String,
    secure: bool,

impl CookieFactory {
    pub fn new_cookie(&self, key: &str, value: &str) -> Cookie {
        let mut cookie = Cookie::new(key.to_string(), value.to_string());

Adding your advised to_string to the parameters, fixed the problem partly.
I still get:

error[E0716]: temporary value dropped while borrowed
  --> src/
80 |         cookie.set_domain(&self.domain.to_string());
   |         -------------------^^^^^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
   |         |                  |
   |         |                  creates a temporary which is freed while still in use
   |         argument requires that borrow lasts for `'static`


Can you maybe help me again?
Thanks already.

Edit: Wrong code and wrong error message.

You need to allocate, note how the parameter must implement Into<Cow<'static, str>>. This means that the parameter can be one of, &'static str, String, Cow<'static, str>. So you can do,


It looks like this is how all of the relevant functions on Cookie work.

Sorry I posted the wrong code and wrong error.
Check my previous post again please.

remove the & in front of self, you need to pass ownership. :slight_smile:

1 Like

Oh did I miss that?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.