// Copyright (C) 2015 by Alexandru Cojocaru // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . struct Point { x: f64, y: f64, } fn dist(a: &Point, b: &Point) -> f64 { ((a.x-b.x).powi(2) + (a.y-b.y).powi(2)).sqrt() } fn mindist<'a>(ps: &'a Vec) -> (&'a Point, &'a Point) { let mut i = 0; let mut min = (&ps[0], &ps[1]); for a in ps { i = i + 1; for b in &ps[i..] { if dist(a, b) < dist(min.0, min.1) { min = (a, b) } } } min } fn main() { let ps: Vec = vec![Point {x: 10.0, y: 10.0}, Point {x: 5.0, y: 5.0}, Point {x: 3.0, y: 2.0}]; let m = mindist(&ps); println!("{} {} {} {}", m.0.x, m.0.y, m.1.x, m.1.y); }