Pass String to 'static, str func

Hi,
I have problem to passing String to a static,str func.

the trait 'std::convert::From<&std::string::String>' is not implemented for 'std::borrow::Cow<'static, str>

could someone help to passing this value.

Can you show the code? Is the String you have non-static I assume? A playground example would be best.

You’ll either need to use an arbitrary lifetime in the Cow, or pass an owned string rather than a reference to one.

1 Like

playground can not run it, because can not support actix crate.
extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate openssl;

fn p404(req: actix_web::HttpRequest) -> Result<actix_web::HttpResponse, actix_web::Error> {
    let x = "a".to_string(); // <---- It can not change to str
    Ok(actix_web::HttpResponse::NotFound().cookie(
                    http::Cookie::build("name", &x) // <---- this method needs str
                        .domain("www.rust-lang.org")
                        .path("/api")
                        .secure(true)
                        .http_only(true)
                        .finish(),
                )
        .content_type("text/plain")
        .body("Not Found"))
}

fn main() {
    if std::env::var("RUST_LOG").is_err() {
        std::env::set_var("RUST_LOG", "actix_web=info");
    }
    env_logger::init();
    let sys = actix::System::new("ws-example");
   actix_web::server::new(|| {
        actix_web::App::new()
            .middleware(actix_web::middleware::Logger::default())
            .handler(
                "/",
                actix_web::fs::StaticFiles::new("./static/").index_file("index.html"),
            )
            .default_resource(|r| {
                r.method(actix_web::http::Method::GET).f(p404);
                r.route()
                    .filter(actix_web::pred::Not(actix_web::pred::Get()))
                    .f(|_| actix_web::HttpResponse::MethodNotAllowed());
            })
    }).unwrap()
        .start();

    println!("Started http server: 127.0.0.1:8443");
    let _ = sys.run();
}

@vitalyd thanks man.
I did use Cow::Owned(x) as Cow<str> and my problem is fixed now, but i'm looking for more efficient way.

FTR: The construction of a Cow itself it nearly free, so if this is inefficient the issue lies elsewhere.
For example, you could just do Cow::Borrowed("a") and skip the String allocation (which occurs in the .to_string() call) altogether.

But if the method in the line http::Cookie::build("name", &x) requires a &str as you say, then perhaps it's best to simply pass the "x" string literal to it directly and skip the Cow altogether.

2 Likes

Thank you for your tips.

But if the method in the line http::Cookie::build("name", &x) requires a &str as you say, then perhaps it’s best to simply pass the "x" string literal to it directly and skip the Cow altogether.

build method requires Cow<'static, str>, so i can not simply pass x, because x is String variable.

1 Like

Ah yes I see what you mean. In that case I recommend the Cow::Borrrowed approach as it avoids the unnnecessary string allocation.

2 Likes

I see you guys have sorted this out, but wanted to mention that you can pass a string literal or an owned string to build since it takes an Into<Cow<'static, str>> generic. IOW, no need to construct the Cow manually.

3 Likes