BoxFuture Lazy Static Function Pointers

⚓ Rust    📅 2025-06-23    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 51 days ago. The information described in this article may have changed.

Hello. I am trying to make a static hashamp with asynchronous function pointers in order to await and call later. I am running into an issue where rust-analyzer is giving this code block all red error signs, and I am not sure what the issue is. Here are the two errors I am getting:
unexpected token in input
expected Expr
Here is my code:

pub type StepFuture = BoxFuture<'static, Result<(), HttpResponse<BoxBody>>>;

lazy_static! {
    static ref WORKDAY_STEPS: HashMap<String, fn() -> StepFuture> = {
        HashMap::from([
            (
                "Create Account/Sign In".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Create Account");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "My Information".to_string(),
                (|| {
                    Box::pin(async {
                        println!("My Info");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "My Experience".to_string(),
                (|| {
                    Box::pin(async {
                        println!("My Exp");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Application Questions".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Application");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Voluntary Disclosures".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Disclosure");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Self Identify".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Identity");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Take Assessment".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Assessment");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Review".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Review");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
        ])
    };
}

If anyone can offer any help or insight, thank you so much.

3 posts - 2 participants

Read full topic

🏷️ rust_feed