Can't get started with Actix-web [Solved]

I was looking into Rust as a full replacement for PHP... But I am not even able to get a basic Actix-web example running...

Using actix-web = "3" already gives the error error: failed to select a version for the requirement actix-web = "^3.0"``.

So I used this for my cargo.toml:

[package]
name = "web_test"
version = "0.1.0"
authors = [""]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-rt = "1.0"
actix-web = "2.0"

And this is my main.rs

use actix_web::{get, middleware, web, App, HttpRequest, HttpResponse, HttpServer, Responder};

#[get("/")]
async fn default() -> impl Responder {
    HttpResponse::Ok().body("Test");
}

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

Errors:

warning: unused imports: `HttpRequest`, `middleware`
 --> src/main.rs:1:22
  |
1 | use actix_web::{get, middleware, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
  |                      ^^^^^^^^^^            ^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0277]: the trait bound `(): actix_web::responder::Responder` is not satisfied
 --> src/main.rs:4:23
  |
4 | async fn default() -> impl Responder {
  |                       ^^^^^^^^^^^^^^ the trait `actix_web::responder::Responder` is not implemented for `()`
  |
  = note: the return type of a function must have a statically known size

error[E0277]: the trait bound `default: actix_web::handler::Factory<_, _, _>` is not satisfied
  --> src/main.rs:12:39
   |
12 |             .route("/", web::get().to(default))
   |                                       ^^^^^^^ the trait `actix_web::handler::Factory<_, _, _>` is not implemented for `default`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `web_test`.

This is just copy/pasted from this documentation page though:

edit:

I was able to fix my code.

use actix_web::{web, App, HttpResponse, HttpServer, Responder};


async fn default() -> impl Responder {
    HttpResponse::Ok().body("Test")
}

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

But I am stil wondering why I can't use actix-web = "3" in my cargo.toml?

Edit:

Using 3.0.0-alpha.3 in my Cargo.toml does compile... But that's not what is specified on the Git-page of Actix-web, is 3.0.0-alpha.3 correct?

PS. I'm started... :stuck_out_tongue:

Git repository may be way newer then the published version, which is at the moment of posting either 2.0.0 (stable) or 3.0.0-beta.1 (unstable).

1 Like

It would be better to get started with proven stable version of actix, instead of unstable alpha/beta version of it. Unstable versions may have bugs impossible to debug for beginners, usability issues, and clearly lacks documentations on internet.

1 Like

If I learn 2.0 now, and make a project in 2.0. Would it be hard to learn 3.0 afterwards and to port my project to 3.0?

Yeah, porting from v2 to v3 would be easier than to develop actix web app from the scratch.

1 Like

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.