Skip to content

Trait bound is not satisfied when attempting to use with_state() #2713

Discussion options

You must be logged in to vote

Hi you need to remove the turbo fish (double colons) next to the .with_state.
And then you need to point the axum::extract::State's generic type thru axum::extract::State<AppState>.

The final code is below:

use axum::routing::get;

#[tokio::main]
async fn main() {
    let shared_state: AppState = AppState { num: 42 };
    let app = setup_routing(shared_state);
    let listener = tokio::net::TcpListener::bind("localhost:5000")
        .await
        .unwrap();
    axum::serve(listener, app).await.unwrap();
}

#[allow(dead_code)]
fn setup_routing(shared_state: AppState) -> axum::Router {
    axum::Router::new()
        .fallback(fallback)
        .nest_service("/static", tower_http::services

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by AnthonysDevAccount
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants