Rust + Iced - Run code when app starts

I have a Rust app that utilizes Iced for the GUI. It works. On the main screen I have a button which sends a message to the Iced update function. The code associated with this message runs code that updates an SQLite database which in turn allows me to update 3 state variables. How can I run this code when the app starts instead of having to click a button? I was unsure what code to include so below is the implementation code for the State struct.

impl State {

    fn new() -> Self {
        Self {
            records_read: 0,
            records_inserted: 0,
            records_updated: 0,
            screen: Screen::HomeScreen,
            closing_values: Vec::new(),
        }
    }

    fn update(&mut self, msg: Message) -> Task<Message> {

        match msg {

            Message::Exit => {
                std::process::exit(0);
            }

            Message::GoToHomeScreen => {
                self.screen = Screen::HomeScreen;
                self.records_read = 0;
                self.records_inserted = 0;
                Task::none()
            }

            Message::GoToUpdateDataBaseScreen => {
                self.screen = Screen::UpdateDatabaseScreen;

                let record_data = match insert_csv_data_into_db() {
                    Ok(data) => {
                        data
                    },
                    Err(e) => {
                        if let IoSqlError::IoError(io_err) = e {
                            println!("I/O error details: {}", io_err);
                        } else if let IoSqlError::SqlError(sql_err) = e {
                            println!("SQL error details: {}", sql_err);
                        }
                        return Task::none();
                    }
                };

                self.records_read = record_data[0];
                self.records_inserted = record_data[1];
                self.records_updated = record_data[2];
                Task::none()
            }

            Message::GoToMinCloseScreen => {

                self.screen = Screen::MinCloseScreen;

                let query_results = match get_close(QueryType::Min) {
                    Ok(results) => results,
                    Err(e) => {
                        if let IoSqlError::IoError(io_err) = e {
                            println!("I/O error details: {}", io_err);
                        } else if let IoSqlError::SqlError(sql_err) = e {
                            println!("SQL error details: {}", sql_err);
                        }
                        return Task::none();
                    }
                };

                self.closing_values = query_results;
                Task::none()
            }

            Message::GoToMaxCloseScreen => {

                self.screen = Screen::MaxCloseScreen;

                let query_results = match get_close(QueryType::Max) {
                    Ok(results) => results,
                    Err(e) => {
                        if let IoSqlError::IoError(io_err) = e {
                            println!("I/O error details: {}", io_err);
                        } else if let IoSqlError::SqlError(sql_err) = e {
                            println!("SQL error details: {}", sql_err);
                        }
                        return Task::none();
                    }
                };

                self.closing_values = query_results;
                Task::none()
            }
        } // end of match statement
    }

    fn view(&self) -> iced::Element<'_, Message> {

        match self.screen {

            Screen::HomeScreen => {
                home_view()
            } 

            Screen::UpdateDatabaseScreen => {
                update_db_view(self.records_read, self.records_inserted, self.records_updated)
            } 

            Screen::MinCloseScreen => {
                min_max_close_view("Min", self.closing_values.as_ref())
            } 

            Screen::MaxCloseScreen => {
                min_max_close_view("Max", self.closing_values.as_ref())
            } 

        } 
    } 
}


You can just run any code you like in State::new, so you can just run insert_csv_data_into_db() in new, no?

I had not thought of that. Will try tomorrow and report. Thank you.

Below is the update State::new code. This works. I also removed the code in the update function associated with updating the database and its associated button, since it is now performed when the app is started.
Thank you for the assistance.

    fn new() -> Self {

        let record_data = match insert_csv_data_into_db() {
            Ok(data) => {
                data
            },
            Err(e) => {
                if let IoSqlError::IoError(io_err) = e {
                    println!("I/O error details: {}", io_err);
                } else if let IoSqlError::SqlError(sql_err) = e {
                    println!("SQL error details: {}", sql_err);
                }
                return Self {
                    records_read: 0,
                    records_inserted: 0,
                    records_updated: 0,
                    screen: Screen::HomeScreen,
                    closing_values: Vec::new(),
                };
            }
        };

        Self {
            records_read: record_data[0],
            records_inserted: record_data[1],
            records_updated: record_data[2],
            screen: Screen::UpdateDatabaseScreen,
            closing_values: Vec::new(),
        }

    }

This refers to the question Rust + Iced - Run code when app starts which is marked as solved and closed.

I had a similar question and I did not like the solution given. The questioner asked if there was a way to auto click a button as part of the Iced application starting in order to initialise a data structure. The solution was to write some more code in State::new() to do it, which duplicates what happens when the button is pressed.

For my use that doesn't really work because I have a bunch of REST calls to make which need an environment that runs Futures etc. I have all that on the button code, and I need to have that as well and I don't want to have it twice. So I looked a bit harder.

The trick is to return a Task as well as the State from the State::new() like this:

    fn new() -> (Self,Task<Message>) {
        
        let t = Task::done(
            Message::DashboardPressed,
        );

        let s = Self {
            screen: Screen::Dashboard,
            layout: Layout::Row,
            spacing: 20,
...
            http_client: HttpClient::new().with_base_url("http://localhost:3091"),
        };
        (s,t)
    }

The Task in t doesn't run any code itself, but it does arrange for the Message::DashboardPressed to run. which is the same message the button sends, and that does the work I need to initialise the rest of the State.

Yes, this is code inside the State::new() but the difference is the code that does the REST calls is not inside the State::new(). The Rest calls are attached to the button and all I am doing here is invoking the button..

The effect is the dashboard loads up as if I had just pressed the button and the various REST calls are done, populating various fields in the State and the view() method is able to render them.