1

I am pretty new to Rust language and trying to make with Actix web framework. Now I want to return following closure from one function

let list = || {
    App::new()
        // enable logger
        .wrap(middleware::Logger::default())
        .service(web::resource("/index.html").to(|| async { "Hello world!" }))
        .service(web::resource("/").to(index::home))
        .service(web::resource("/h2").to(index::home2))
};

So, I can consume that with

HttpServer::new(routes::list())
.bind("127.0.0.1:8080")?
.run()
.await

So, what will be the signature of Rust function will be?

Amber More
  • 131
  • 1
  • 15
  • 1
    I don't see a function in your code. Please edit your question to include a [reprex] to illustrate the problem you are facing. – Peter Hall May 30 '20 at 10:42

1 Answers1

1

This is what impl trait is for: the closure type itself doesn't have a name, but it allows you to say "function returns some type satisfying these trait bounds" without naming the type. So it should be

fn list() -> impl Fn() -> ???

Where ??? should be replaced by the return type. See "impl Trait and closures" section in the link for another example.

But note that there still must be a single return type, so you can't e.g. return one of several closures depending on some condition, their types are different!

mcarton
  • 27,633
  • 5
  • 85
  • 95
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • I can mark an answer to this, but the problem is this when I do your suggested way like --- fn list() -> impl Fn() -> actix_web::app::App. It shows me AppEntry and Body are private members. Any another way to return Actix App ? – Amber More Jun 06 '20 at 12:08
  • I don't know Actix, but it doesn't seem so: https://github.com/actix/actix-web/issues/780, https://github.com/actix/actix-web/issues/1005. – Alexey Romanov Jun 06 '20 at 13:38
  • For reference, there is a .configure method on routing table structs that can delegate to a function for setting up (sub)routes. Doc page https://actix.rs/docs/application/ – Amber More Jun 19 '20 at 14:50