rust/src/libcore/tuple.rs

29 lines
623 B
Rust
Raw Normal View History

2012-03-15 20:58:14 -05:00
#[doc = "Operations on tuples"];
#[doc = "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;
}
2012-03-15 20:58:14 -05:00
#[doc = "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;
}
2012-03-15 20:58:14 -05:00
#[doc = "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";
assert swap(('a', 2)) == (2, 'a');
}