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