1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
16 fn plus(&self) -> int;
21 impl plus for uint { fn plus(&self) -> int { *self as int + 20 } }
26 impl plus for ~str { fn plus(&self) -> int { 200 } }
30 fn str(&self) -> ~str;
31 fn multi(&self, f: |uint|);
34 impl uint_utils for uint {
35 fn str(&self) -> ~str { self.to_str() }
36 fn multi(&self, f: |uint|) {
38 while c < *self { f(c); c += 1u; }
43 fn length_(&self, ) -> uint;
44 fn iter_(&self, f: |&T|);
45 fn map_<U>(&self, f: |&T| -> U) -> Vec<U> ;
48 impl<T> vec_utils<T> for Vec<T> {
49 fn length_(&self) -> uint { self.len() }
50 fn iter_(&self, f: |&T|) { for x in self.iter() { f(x); } }
51 fn map_<U>(&self, f: |&T| -> U) -> Vec<U> {
52 let mut r = Vec::new();
53 for elt in self.iter() {
61 assert_eq!(10u.plus(), 30);
62 assert_eq!((~"hi").plus(), 200);
64 assert_eq!((vec!(1)).length_().str(), ~"1");
65 let vect = vec!(3, 4).map_(|a| *a + 4);
66 assert_eq!(*vect.get(0), 7);
67 let vect = (vec!(3, 4)).map_::<uint>(|a| *a as uint + 4u);
68 assert_eq!(*vect.get(0), 7u);
70 10u.multi(|_n| x += 2u );