Need help with Rust & Actix Connection

So I was following the instructions on https://actix.rs/docs/getting-started/
and I reach the point where it says I can check out what's been done by going to http://localhost:8088/
I head there and it says that the site can't be reached.

I call this function through main and .toml looks good.

This is my code:

pub fn actix()
{
    use actix_web::{web, App, HttpResponse, HttpServer, Responder};

    async fn index() -> impl Responder
    {
        HttpResponse::Ok().body("Hello world!")
    }

    async fn index2() -> impl Responder
    {
        HttpResponse::Ok().body("Hello world again!")
    }

    #[actix_rt::main]
    async fn main() -> std::io::Result<()>
    {
        HttpServer::new(||
        {
            App::new()
            .route("/", web::get().to(index))
            .route("/again", web::get().to(index2))
        })
        .bind("127.0.0.1:8088")?
        .run()
        .await
    }
}

What did I do wrong?

Please fix the code formatting of your post.

```
// your code here
```

Thank you for letting me know how to format the code.

You’ve wrapped the main function in another function. So main is likely never called. You probably want the main function as the top level main function for the binary. You can move the server creation to a separate function if you don’t want to do it directly in main.

Thank you for your guidance; I call pub fn actix() through a main function in a different file; I guess it's not possible to have more than one main function at a time. I thought that my main function in a different file ends up calling fn actix which in turn calls its internal main function and run async fn main. On this note, how do you manually call an async function?

If you look at the source for actix_rt::main all its doing is the following, where #body is the the code in main.

actix_rt::System::new("main").block_on(async move { #body })

And then anything in body can call async code as it wishes. The code won't return though until the code block is finished, so you'd need to start it on a separate thread if you want to do other things in your program.

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.