Skip to content

toggle

Removes item from items if it is there, adds it at the end if it is not: the usual “toggle a selection” gesture. Only the first occurrence is removed.

use helpers4::array::toggle;

Cargo feature array (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features array

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.9", default-features = false, features = ["array"] }
pub fn toggle<T: Clone + PartialEq>(items: &[T], item: T) -> Vec<T>
ParameterTypeDescription
items&[T]The current elements.
itemTThe element to toggle, compared with ==.

Vec<T> — A new Vec; items is not changed.

use helpers4::array::toggle;

assert_eq!(toggle(&[1, 2, 3], 2), vec![1, 3]);
assert_eq!(toggle(&[1, 3], 2), vec![1, 3, 2]);

src/array/toggle.rs