Examples · 22 of 339
22-tuples
Tuples group a few values without declaring a type: (lo, hi), ("x", 1.5).
tin run examples/22-tuples.tin// Tuples group a few values without declaring a type: (lo, hi), ("x", 1.5).
// Read items with .0, .1, or bind them all at once with let (a, b) = ...
// (This example also shows a guarded match arm.)
fn bounds(xs: float[]): (float, float) {
var lo = xs[0]
var hi = xs[0]
for x in xs {
if x < lo { lo = x }
if x > hi { hi = x }
}
(lo, hi)
}
fn main() {
let (lo, hi) = bounds([0.4, -1.2, 2.5, 0.0])
print("joint range {lo} .. {hi}")
let limits: (string, float)[] = [("shoulder", 1.6), ("elbow", 2.1)]
for i, limit in limits {
// a match arm can carry a guard: it matches only when the condition holds
let note = match limit.1 {
r if r > 2.0 => "wide"
_ => "narrow"
}
print("{i}: {limit.0} up to {limit.1} rad ({note})")
}
} joint range -1.2 .. 2.5
0: shoulder up to 1.6 rad (narrow)
1: elbow up to 2.1 rad (wide)