Skip to content

Instantly share code, notes, and snippets.

@tckmn
Created August 2, 2016 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tckmn/368fa4bdb5ca9b066a3ff8270203a4f1 to your computer and use it in GitHub Desktop.
Save tckmn/368fa4bdb5ca9b066a3ff8270203a4f1 to your computer and use it in GitHub Desktop.
#![feature(try_from)]
use std::{ops,convert,clone,fmt};
fn generic_odds<T>(v: &Vec<T>) -> Vec<T>
where T: ops::Rem<T> + convert::TryFrom<u8> + clone::Clone,
<T as ops::Rem<T>>::Output: PartialEq<T>,
<T as convert::TryFrom<u8>>::Err: fmt::Debug {
v.iter().cloned().filter(|elt| {
elt.clone() % T::try_from(2u8).unwrap() == T::try_from(1u8).unwrap()
}).collect()
}
fn odds(v: &Vec<i32>) -> Vec<i32> {
v.iter().cloned().filter(|&elt| elt % 2 == 1).collect()
}
fn main() {
let v = vec![1,2,3,4,5];
println!("{:?}", odds(&v));
let v2: Vec<u64> = vec![1,2,3,4,5];
println!("{:?}", generic_odds(&v2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment