Advertisement
NLinker

split_filter

May 25th, 2018
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.47 KB | None | 0 0
  1. // https://stackoverflow.com/a/32619597/5066426
  2. fn main() {
  3.     let v = vec![10, 40, 30, 20, 60, 50];
  4.     println!("{:?}", split_filter(&v, |&a| a % 3 == 0));
  5. }
  6.  
  7. fn split_filter<T: Clone, F>(a: &[T], f: F) -> (Vec<T>, Vec<T>)
  8.     where F: Fn(&T) -> bool
  9. {
  10.     let mut i: Vec<T> = vec![];
  11.     let mut e: Vec<T> = vec![];
  12.     for u in a.iter().cloned() {
  13.         if f(&u) {
  14.             i.push(u)
  15.         } else {
  16.             e.push(u)
  17.         }
  18.     }
  19.     (i, e)
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement