I am trying out a sample code from a portal to build REST API using actix-web. It compiles fine but when I run this I get the error:
thread 'main' panicked at 'Actix or Tokio runtime not found; halting', C:\Users\myuserid\.cargo\registry\src\github.com-1ecc6299db9ec823\actix-server-2.3.0\src\server.rs:198:27
Here is the full code from two files, i.e. main.rs and handlers.rs.
// src/handlers.rs
use actix_web::Responder;
pub async fn get_users() -> impl Responder {
format!("hello from get users")
}
pub async fn get_user_by_id() -> impl Responder {
format!("hello from get users by id")
}
pub async fn add_user() -> impl Responder {
format!("hello from add user")
}
pub async fn delete_user() -> impl Responder {
format!("hello from delete user")
}
I am new to Rust. Not sure why I get this error. I tried adding tokio to Cargo.toml and also tried adding use actix_rt::Runtime; to top of the main.rs but it didn’t help.
Yes, it compiled for me too. Thanks a lot for your help. The primary difference was use of older version of actix-rt in my Cargo.toml which I copied from the two year old article.
actix-rt = "1.0.0" => should be 2.9.0
The actix-web version was also old but I found and updated it to latest. Missed this.
By the way, actix-web re-exports the main macro (as actix_web::main) and the relevant types from actix-rt (in the actix_web::rt module). If you use those you can remove actix-rt from your Cargo.toml and you won't have to care about continuously updating actix-rt to the correct version for your actix-web.
Thanks for sharing that info @SkiFire13. I am still trying to grasp this. I and a colleague found another example code about four months back for building REST API. It was old, we could change a few lines and get it to compile and work but the moment we touched the dependencies it used to stop compiling. We tried hard but couldn't get it to work. It was compiling but not running on Windows but running on Linux. We have been facing all these kinds of challenges.
I want to know, if it is ok to use the latest version of each package for any application. I don't know how Rust handles a situation like this, hypothetical question:
An application depends on packages A, B, X & Y.
Package A depends on 0.5.0 of X
Package B depends on 0.7.0 of X
How does Rust manage the compilation if 0.5.0 and 0.7.0 are incompatible. Will it use both of them? Or does it store & use only one version of one package in the build directory?
In addition, types included from different major versions of a crate will be considered completely different types, and therefore incompatible. This is a common cause of weird compiler errors in the form "Expected Foo but found Foo".