How do I use an if or match statement and ignore all but one condition being true

⚓ Rust    📅 2026-05-08    👤 surdeus    👁️ 2      

surdeus

I have a rust / iced program in which I manually create a spreadsheet type table into which I place data. All works fine with one exception. When I get to the last row I need to insert a line via a widget. I can test for the last row with an if statement but rust requires that I include an else statement and that it returns the same type. The match statement appears to work the same way. How do I test for the end of the data / table and do nothing when it is not? Any assistance will be appreciated.

    let num_elements = closing_by_date.len() - 1;

    for (index, result) in closing_by_date.iter().enumerate() {

        println!("index = {}", index);
        col1 = col1.push(

            widget::column![
                widget::rule::horizontal(1).style(horz_rule),
                widget::row![
                    widget::rule::vertical(1).style(horz_rule),
                    widget::container(
                        widget::text(&result.date)
                            .size(17)
                            .font(date_font)
                    ) // end of inner container
                    .align_right(125.0)
                    .padding(right(8.0))
                    .center_y(cell_height),
                ], // end of row

// ##############################
// the if statement below is the problem                
                if index == num_elements {
                    widget::rule::horizontal(1).style(horz_rule)
                } else { }, 

            ] // end of column
            .height(Length::Fixed(cell_height))
            .width(Length::Fixed(125.0))
        );

        col2 = col2.push(

            widget::column![
                widget::rule::horizontal(1).style(horz_rule),
                    widget::row![
                        widget::rule::vertical(1).style(horz_rule),
                        widget::container(
                            widget::text(&result.close)
                                .size(17)
                                .font(money_font)
                        ) // end of container
                        .center_y(cell_height)
                        .align_left(90.0)
                        .padding(left(10.0)),
                    ]
            ] // end of column
            .height(Length::Fixed(cell_height))
            .width(Length::Fixed(90.0))
        ); // end col2 push

        col3 = col3.push(

            widget::column![
                widget::rule::horizontal(1).style(horz_rule),
                widget::row![
                    widget::rule::vertical(1).style(horz_rule),
                    widget::container(
                        widget::text(&result.minormax)
                            .size(17.0)
                            .font(date_font),
                    )
                    // .align_right(60.0)
                    // .padding(right(10.0))
                    .center_x(60.0)
                    .center_y(cell_height),
                    // .align_left(60)
                    // .padding(left(8.0)),
                    widget::rule::vertical(1).style(horz_rule),                            
                ] // end of row
            ] // end of column
            .height(Length::Fixed(cell_height))
            .width(Length::Fixed(62.0))
        );

    }    

4 posts - 3 participants

Read full topic

🏷️ Rust_feed