“Clone”特性未为”actix_web::Scope”实现。

huangapple go评论73阅读模式
英文:

the trait `Clone` is not implemented for `actix_web::Scope`

问题

我想将我的应用程序路由分组在范围内,以便将来可以根据领域分离它们的文件位置。
我试图做的是将

HttpServer::new(move || App::new().app_data(app_state.clone()).service(delete_comment).service(update_comment).service(get_comments).service(create_comment))
        .bind("127.0.0.1", 8080)?
        .run()
        .await

转换为:

let comment_scope = web::scope("/comments").service(delete_comment).service(update_comment).service(get_comments).service(create_comment);
HttpServer::new(move || App::new().app_data(app_state.clone()).service(comment_scope))
        .bind("127.0.0.1", 8080)?
        .run()
        .await

但它一直告诉我,actix_web::Scope 没有实现 Clone 特性。
我该如何解决这个问题?

英文:

I want to group my app routes in scope, so I can separate their file location per Domain in the future.
what I'm trying to do is convert

HttpServer::new(move || App::new().app_data(app_state.clone()).service(delete_comment).service(update_comment).service(get_comments).service(create_comment))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await

to:

let comment_scope =  web::scope("/comments").service(delete_comment).service(update_comment).service(get_comments).service(create_comment);
HttpServer::new(move || App::new().app_data(app_state.clone()).service(comment_scope))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await

but it keeps telling me that the trait Clone is not implemented for actix_web::Scope.
how can I fix this?

答案1

得分: 5

你只需将 comment_scope 声明移到 new 闭包中:

HttpServer::new(move || {
    let comment_scope = web::scope("/comments")
        .service(delete_comment)
        .service(update_comment)
        .service(get_comments)
        .service(create_comment);

    App::new()
        .service(comment_scope)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
英文:

You just have to move the comment_scope declaration into new closure:

HttpServer::new(move || {
      
        let comment_scope = web::scope("/comments")
        .service(delete_comment)
        .service(update_comment)
        .service(get_comments)
        .service(create_comment);

        App::new()
            .service(comment_scope)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await

答案2

得分: 0

使用TcpListener来尝试这个:

use std::net::TcpListener;
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
    let server = HttpServer::new(|| {
        App::new()
            .route("/comments", web::post().to(comments))
            [/.../]
    })
        .listen(listener)?
        .run();
    Ok(server)
}
英文:

How about try this by using TcpListener:

use std::net::TcpListener;
pub fn run(listener: TcpListener) -&gt; Result&lt;Server, std::io::Error&gt; {
    let server = HttpServer::new(|| {
        App::new()
            .route(&quot;/comments&quot;, web::post().to(comments))
            [/.../]
    })
        .listen(listener)?
        .run();
    Ok(server)
}

huangapple
  • 本文由 发表于 2023年6月4日 23:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定