drop_right_while

Function drop_right_while 

Source
pub fn drop_right_while<T, F>(collection: &[T], predicate: F) -> Vec<T>
where T: Clone, F: Fn(&T) -> bool,
Expand description

Removes elements from the end of a collection as long as a predicate returns true, and returns the remaining elements. As soon as the predicate returns false, the function stops dropping elements.

Time Complexity: O(n), where n is the number of elements until the predicate returns false.

§Arguments

  • collection - A slice of items from which elements will be dropped.
  • predicate - A function that takes an item and returns true or false.

§Type Parameters

  • T - The type of elements in the collection. Must implement Clone.
  • F - The type of the predicate function. Must implement Fn(&T) -> bool.

§Returns

  • Vec<T> - A vector containing the elements after dropping from the end while the predicate holds true.

§Examples

use lowdash::drop_right_while;

let numbers = vec![1, 2, 3, 4, 5];
let result = drop_right_while(&numbers, |&x| x > 3);
assert_eq!(result, vec![1, 2, 3]);
use lowdash::drop_right_while;

let letters = vec!['a', 'b', 'c', 'd', 'e'];
let result = drop_right_while(&letters, |&c| c != 'c');
assert_eq!(result, vec!['a', 'b', 'c']);