Created
August 2, 2016 13:58
-
-
Save tckmn/368fa4bdb5ca9b066a3ff8270203a4f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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