summary refs log tree commit diff
path: root/src/libcore/tuple.rs
blob: 98840dadc1115083e43d8242bca6aa44a2232d91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#[doc = "Operations on tuples"];

#[doc = "Return the first element of a pair"]
pure fn first<T:copy, U:copy>(pair: (T, U)) -> T {
    let (t, _) = pair;
    ret t;
}

#[doc = "Return the second element of a pair"]
pure fn second<T:copy, U:copy>(pair: (T, U)) -> U {
    let (_, u) = pair;
    ret u;
}

#[doc = "Return the results of swapping the two elements of a pair"]
pure fn swap<T:copy, U:copy>(pair: (T, U)) -> (U, T) {
    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');
}