Compile Error with Tokio and Actix-Web in Rust #13
Answered
by
pranjalpruthi
watchdogbro
asked this question in
Q&A
Replies: 2 comments 7 replies
-
This error usually occurs when you're trying to capture Try changing your Example (using a reference): async fn handle_request(&self) -> Result<HttpResponse, Error> {
// ... your code ...
}
// In main:
tokio::spawn(async {
// ... call handle_request using a reference to your struct ...
}); |
Beta Was this translation helpful? Give feedback.
3 replies
-
Another common cause for this error is that you are using a non-static closure inside the tokio::spawn.
Try using a move closure, and make sure that all captured variables are owned by the closure.
```rust
tokio::spawn(async move {
let owned_data = your_data.clone(); // Clone if needed
// ... use owned_data ...
}); |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
watchdogbro
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm encountering a compile error when trying to combine Tokio and Actix-Web in my Rust project. The error seems to be related to async context and lifetime issues.
Details:
Setup:
cargo --version
cargo 1.68.0 (ec4fa3db5 2023-03-09)
rustc --version
rustc 1.68.0 (2c8cc3432 2023-03-09)
Cargo.toml dependencies:
actix-web = "4"
tokio = { version = "1", features = ["full"] }
Encountered compile error:
cargo build
error[E0759]:
self
has an anonymous lifetime'_
but it needs to satisfy a'static
lifetime requirement--> src/main.rs:20:10
|
20 | tokio::spawn(async move {
| ^^^^^^^
|
note: lifetime annotations needed
--> src/main.rs:18:31
|
18 | async fn handle_request(self) -> Result<HttpResponse, Error> {
| ^^^^
How can I resolve this lifetime issue and correctly use Tokio with Actix-Web?
Beta Was this translation helpful? Give feedback.
All reactions