-
Notifications
You must be signed in to change notification settings - Fork 164
Open
Description
For example, when the maximum number of selections is reached, users can't select more items unless they first deselect some of their current choices.
// ==============================================
// Limit maximum selections
// ==============================================
let max_selections = 2; // Set maximum number of selections
loop {
let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select your favorite foods (max 2 items)")
.items(&foods[..])
.defaults(&defaults[..])
.max_length(2)
.interact()
.unwrap();
if selections.len() <= max_selections {
// Process valid selections
if selections.is_empty() {
println!("You didn't select any food :(");
} else {
println!("You selected:");
for index in selections {
println!(" - {}", foods[index]);
}
}
break;
} else {
println!("You can select at most {} items. Please try again.", max_selections);
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels