Why does my application crash?

I am trying to make a way to iterate over a vector of text inputs, create a text field to each input and link that text field to the content by index. When I build my program, the compile does not raise any error.
When I run the code, the code crashs. I am not sure what I am doing wrong here.
here is my code:

use iced::widget::{button, column, text, Column, container, row, text_input};

#[derive(Debug, Clone)]
pub enum Message {
    ContentChanged(String, usize),
}
#[derive(Default)]
struct App {
    content: Vec<String>,
}

struct TextInputs {
    title: String,
    placeholder: String,
}

impl TextInputs {
    pub fn new(title: String, placeholder: String) -> TextInputs {
        TextInputs {
            title,
            placeholder,
        }
    }
}

impl App {
    pub fn view(&self) -> Column<Message> {
        // We use a column: a simple vertical layout
        let mut col = column![];
        for (idx, obj) in vec![
            TextInputs::new("Name".to_string(), "Enter your name...".to_string()),
            TextInputs::new("Location".to_string(), "Enter your location...".to_string()),
            TextInputs::new("Phone".to_string(), "Enter your phone number...".to_string()),
            TextInputs::new("Email".to_string(), "Enter your email...".to_string()),
        ].into_iter().enumerate() {
            let row = row!(
                text(obj.title),
                text_input(obj.placeholder.as_str(), &self.content[idx])
                .on_input(move |_| Message::ContentChanged(self.content[idx].clone(), idx)));
            col = col.push(row)
        }
        col.into()
    }
    pub fn update(&mut self, message: Message) {
        match message {
            Message::ContentChanged(content, idx) => {
                self.content[idx] = content;
            }
        }
    }
}

fn main() -> iced::Result {
    iced::run("A cool example", App::update, App::view)
}

Run your app and it will tell you why it’s crashing with a very helpful error message, including the line where the error happened exactly.

Hi, when App is initialized, content is initialized as an empty vector. But then on line 41, you try to access this vector as if it held some content, which is not the case.

1 Like

Thank you. Is there a way to initialize App with data prior to the iced::run(…) call. Eg:

 let test = App {
        content: Vec::with_capacity(4)
};
// Use test somehow as the app for iced::run
iced::run("A cool example", App::update, App::view)

The easiest answer is to define a custom Default implementation for your app so that those fields are created when App is created

The longer answer is that you shouldn’t be creating any data in fn view. That’s why &self is borrowed immutably.

The extra credit answer is you can create data asynchronously on init using iced::application(...).run_with() instead of iced::application(...).run() for those cases when a custom implementation of Default won’t suffice. But you don’t need that here.

1 Like

Thanks. When I meant by crash, was that my computer got a quit unexpectedly and an aborted message. Would you like me to copy and paste the error messages?

No, I already know the message. I’m suggesting you read the message and perhaps try to repeat what it’s saying in your own words so that you learn how to debug this yourself. It’s a fundamental step in your development