2018-08-30 14:18:55 +02:00
|
|
|
// run-pass
|
2018-08-31 15:02:01 +02:00
|
|
|
#![allow(non_camel_case_types)]
|
2014-03-05 15:28:08 -08:00
|
|
|
|
2015-03-22 13:13:15 -07:00
|
|
|
|
2013-07-09 11:14:17 -07:00
|
|
|
trait vec_utils<T> {
|
2015-01-02 17:32:54 -05:00
|
|
|
fn map_<U, F>(x: &Self, f: F) -> Vec<U> where F: FnMut(&T) -> U;
|
2013-07-09 11:14:17 -07:00
|
|
|
}
|
|
|
|
|
2014-03-05 14:02:44 -08:00
|
|
|
impl<T> vec_utils<T> for Vec<T> {
|
2015-01-02 17:32:54 -05:00
|
|
|
fn map_<U, F>(x: &Vec<T> , mut f: F) -> Vec<U> where F: FnMut(&T) -> U {
|
2014-03-05 14:02:44 -08:00
|
|
|
let mut r = Vec::new();
|
2015-01-31 12:20:46 -05:00
|
|
|
for elt in x {
|
2013-07-09 11:14:17 -07:00
|
|
|
r.push(f(elt));
|
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:43:37 -07:00
|
|
|
pub fn main() {
|
2016-10-29 22:54:04 +01:00
|
|
|
assert_eq!(vec_utils::map_(&vec![1,2,3], |&x| x+1), [2,3,4]);
|
2013-07-09 11:14:17 -07:00
|
|
|
}
|