How to calculate the Euclidean distance between two points in Rust

1 Answer

0 votes
// The Euclidean distance is a measure of the straight-line distance 
// between two points in a 2D or 3D space

use std::f64;

// CalculateEuclideanDistance computes the Euclidean distance between two 2D points
fn calculate_euclidean_distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
    ((x2 - x1).powi(2) + (y2 - y1).powi(2)).sqrt()
}

fn main() {
    let x1 = 3.0;
    let y1 = 4.0;
    let x2 = 5.0;
    let y2 = 9.0;

    let distance = calculate_euclidean_distance(x1, y1, x2, y2);
    println!("Euclidean Distance: {:.5}", distance);
}



/*
run:

Euclidean Distance: 5.38516

*/

 



answered Oct 13, 2025 by avibootz
...