Issue pushing objects in column

I was attempting to create a row of several text objects in a loop:

    fn view(&self) -> Element<Message> {
        let height = 7;
        let mut col: Column<'_, Message> = column![];

        for num in 0..height {
            col.push(text!(""));
        }

        col.into()
    }

However col falls out of scope after the loop. This is because the column push function uses self instead of &mut self. Is this intentional behavior? Is there a better way to achieve this affect?

col = col.push(text!("")); is what you’re looking for

but also the column() function from use iced::widget::column; takes an impl IntoIterator so you could just do

let col = column((0..height).map(|_| text!("").into())).into()

See Column in iced::widget - Rust and column in iced::widget - Rust

1 Like