2013-07-09 13:14:17 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
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
|
|
|
}
|