|
1 | 1 | //! Operations on tuples
|
2 | 2 |
|
3 |
| -/// Return the first element of a pair |
4 |
| -pure fn first<T:copy, U:copy>(pair: (T, U)) -> T { |
5 |
| - let (t, _) = pair; |
6 |
| - ret t; |
7 |
| -} |
8 | 3 |
|
9 |
| -/// Return the second element of a pair |
10 |
| -pure fn second<T:copy, U:copy>(pair: (T, U)) -> U { |
11 |
| - let (_, u) = pair; |
12 |
| - ret u; |
13 |
| -} |
| 4 | +impl extensions <T:copy, U:copy> for (T, U) { |
| 5 | + |
| 6 | + /// Return the first element of self |
| 7 | + pure fn first() -> T { |
| 8 | + let (t, _) = self; |
| 9 | + ret t; |
| 10 | + } |
| 11 | + |
| 12 | + /// Return the second element of self |
| 13 | + pure fn second() -> U { |
| 14 | + let (_, u) = self; |
| 15 | + ret u; |
| 16 | + } |
| 17 | + |
| 18 | + /// Return the results of swapping the two elements of self |
| 19 | + pure fn swap() -> (U, T) { |
| 20 | + let (t, u) = self; |
| 21 | + ret (u, t); |
| 22 | + } |
14 | 23 |
|
15 |
| -/// Return the results of swapping the two elements of a pair |
16 |
| -pure fn swap<T:copy, U:copy>(pair: (T, U)) -> (U, T) { |
17 |
| - let (t, u) = pair; |
18 |
| - ret (u, t); |
19 | 24 | }
|
20 | 25 |
|
21 | 26 |
|
22 | 27 | #[test]
|
23 | 28 | fn test_tuple() {
|
24 |
| - assert first((948, 4039.48)) == 948; |
25 |
| - assert second((34.5, ~"foo")) == ~"foo"; |
26 |
| - assert swap(('a', 2)) == (2, 'a'); |
| 29 | + assert (948, 4039.48).first() == 948; |
| 30 | + assert (34.5, ~"foo").second() == ~"foo"; |
| 31 | + assert ('a', 2).swap() == (2, 'a'); |
27 | 32 | }
|
28 | 33 |
|
0 commit comments