Skip to content

How to store a state in Rust #240

Answered by temeddix
milesegan asked this question in Q&A
Dec 11, 2023 · 1 comments · 5 replies
Discussion options

You must be logged in to vote

Hi @milesegan , thank you for your participation!

Your code might work, but I recommend using OnceCell instead. If you use OnceCell, your conn becomes a global variable that you can access from anywhere via crate::conn_cell.

use std::cell::OnceCell;

let conn_cell = OnceCell::new();

async fn main() {
    let conn = db::create_connection();
    conn_cell.set(conn);
    let mut request_receiver = bridge::get_request_receiver();
    while let Some(request_unique) = request_receiver.recv().await {
        tokio::spawn(async {
            let response_unique = handle_request(request_unique).await;
            respond_to_dart(response_unique);
        });
    }
}

async fn some_other_function() {

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@milesegan
Comment options

@temeddix
Comment options

@temeddix
Comment options

@milesegan
Comment options

@temeddix
Comment options

Answer selected by milesegan
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