Flags to application

I am developing a application which takes arguments from user and the application depends on it. Till version 0.12.1, I could just implement Application trait and the new function will take flags which sorts out the problem. But as I was updating my app to master branch of iced, I observed Application trait has been removed, how to handle this now? I know I can make static variables using OnceLock and my argument parser could call init on it and my iced application could read the static variable, but it makes the code clumsy. How to achieve this? The data flow from argument parser to my struct new function is linear.

You can use the Application::run_with function. It lets you create an application with the state that you provide as an argument.

Hi, I have tried the same, it doesn’t take arguments. My struct’s new function needs to take arguments which are taken from Argument Parser

It results in:


error[E0593]: function is expected to take 0 arguments, but it takes 1 argument
   --> src/main.rs:41:19
    |
41  |         .run_with(Bounds::new)
    |          -------- ^^^^^^^^^^^ expected function that takes 0 arguments
    |          |
    |          required by a bound introduced by this call
    |
   ::: src\bounds.rs:39:5
    |
39  |     pub fn new(path: String) -> (Self, Task<Message>) {
    |     ------------------------------------------------------- takes 1 argument
    |
note: required by a bound in `Application::<P>::run_with`
   --> ...\src\application.rs:172:12
    |
169 |     pub fn run_with<I>(self, initialize: I) -> Result
    |            -------- required by a bound in this associated function
...
172 |         I: FnOnce() -> (P::State, Task<P::Message>) + 'static,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Application::<P>::run_with`

I made a small example for you:

#[derive(Debug)]
struct State {
    text: String,
}

#[derive(Debug, Clone)]
enum Message {}

impl State {
    fn update(&mut self, _message: Message) -> iced::Task<Message> {
        iced::Task::none()
    }

    fn view(&self) -> iced::Element<Message> {
        iced::widget::text(&self.text).into()
    }
}

fn main() {
    let text = String::from("Example");

    iced::application("Title", State::update, State::view)
        .run_with(|| (State { text }, iced::Task::none()))
        .unwrap();
}

I pass a closure to the run_with function that creates the initial state. The closure can takes ownership of the variable text and uses it to create the state.

Oops, how could I miss that. Thanks a ton buddy