rust/src/libcore/tuple.rs

29 lines
596 B
Rust
Raw Normal View History

//! Operations on tuples
2012-03-15 20:58:14 -05:00
/// Return the first element of a pair
2012-03-15 15:57:26 -05:00
pure fn first<T:copy, U:copy>(pair: (T, U)) -> T {
2012-01-17 12:14:05 -06:00
let (t, _) = pair;
ret t;
}
/// Return the second element of a pair
2012-03-15 15:57:26 -05:00
pure fn second<T:copy, U:copy>(pair: (T, U)) -> U {
2012-01-17 12:14:05 -06:00
let (_, u) = pair;
ret u;
}
/// Return the results of swapping the two elements of a pair
2012-03-15 15:57:26 -05:00
pure fn swap<T:copy, U:copy>(pair: (T, U)) -> (U, T) {
2012-01-17 12:14:05 -06:00
let (t, u) = pair;
ret (u, t);
}
#[test]
fn test_tuple() {
assert first((948, 4039.48)) == 948;
assert second((34.5, ~"foo")) == ~"foo";
2012-01-17 12:14:05 -06:00
assert swap(('a', 2)) == (2, 'a');
}