2026-09-16
midpoint.rs
// write this: a function `midpoint` that takes two points, each an
// (f64, f64) tuple, destructured directly as parameter patterns (no
// `let` inside the body), and returns their midpoint as a tuple
2つの点をそれぞれ(f64, f64)のタプルとして受け取り、中点を返すmidpointを書いてください。関数本体でletによるタプルの分解はせず、仮引数の位置でパターンとして受け取ること。
Reference
fn midpoint((x1, y1): (f64, f64), (x2, y2): (f64, f64)) -> (f64, f64) {
((x1 + x2) / 2.0, (y1 + y2) / 2.0)
}
Rustの仮引数は単なる名前ではなく、パターンとして書けます。実引数として渡されたタプルは呼び出しの時点でそのままこの位置に束縛されるので、x1 y1 x2 y2は関数本体に入った時点ですでに分解済みです。本体で改めてlet (x1, y1) = p1;のように書き直す必要がありません。これは参考実装であり、唯一の正解ではありません。