Repository: rust-lang/rustlings
File: src/list.rs (Rust)

Code before:
    loop {
        match event::read().context("Failed to read terminal event")? {
            Event::Key(key) => {
                match key.kind {
                    KeyEventKind::Release => continue,
                    KeyEventKind::Press | KeyEventKind::Repeat => (),
                }

                list_state.message.clear();

                let curr_key = key.code;

                if is_searching {
                    match curr_key {
                        KeyCode::Esc | KeyCode::Enter => {
                            is_searching = false;
                            list_state.search_query.clear();
                            continue;
                        }
                        KeyCode::Char(k) => {
                            list_state.search_query.push(k);
                            list_state.message.push_str("search:");
                            list_state.message.push_str(&list_state.search_query);
                            list_state.message.push('|');

                            list_state.select_if_matches_search_query();

                            list_state.draw(stdout)?;
                            continue;
                        }
                        KeyCode::Backspace => {
                            list_state.search_query.pop();
                            list_state.message.push_str("search:");
                            list_state.message.push_str(&list_state.search_query);
                            list_state.message.push('|');

                            list_state.select_if_matches_search_query();

                            list_state.draw(stdout)?;
                            continue;
                        }
                        _ => {
                            continue;
                        }
                    }
                }

                match key.code {
                    KeyCode::Char('q') => {
                        return Ok(());
                    }

Review comment (refactor):
Updating the message and then calling `select_if_matches_search_query` is duplicated. It would be better to replace `select_if_matches_search_query` with something like `apply_search_query` which also updates the message.

Code after:
    loop {
        match event::read().context("Failed to read terminal event")? {
            Event::Key(key) => {
                match key.kind {
                    KeyEventKind::Release => continue,
                    KeyEventKind::Press | KeyEventKind::Repeat => (),
                }

                list_state.message.clear();

                let curr_key = key.code;

                if is_searching {
                    match curr_key {
                        KeyCode::Esc | KeyCode::Enter => {
                            is_searching = false;
                            list_state.search_query.clear();
                        }
                        KeyCode::Char(k) => {
                            list_state.search_query.push(k);
                            list_state.apply_search_query();
                            list_state.draw(stdout)?;
                        }
                        KeyCode::Backspace => {
                            list_state.search_query.pop();
                            list_state.apply_search_query();
                            list_state.draw(stdout)?;
                        }
                        _ => {}
                    }
                    continue;
                }

                match key.code {
                    KeyCode::Char('q') => return Ok(()),
                    KeyCode::Down | KeyCode::Char('j') => list_state.select_next(),
                    KeyCode::Up | KeyCode::Char('k') => list_state.select_previous(),
                    KeyCode::Home | KeyCode::Char('g') => list_state.select_first(),
                    KeyCode::End | KeyCode::Char('G') => list_state.select_last(),
                    KeyCode::Char('d') => {
                        if list_state.filter() == Filter::Done {
                            list_state.set_filter(Filter::None);
                            list_state.message.push_str("Disabled filter DONE");
                        } else {
                            list_state.set_filter(Filter::Done);
                            list_state.message.push_str(
                                "Enabled filter DONE │ Press d again to disable the filter",
                            );
                        }
                    }
                    KeyCode::Char('p') => {