find the distance between two points in rust

To find the distance between two points in rust, we can use the Euclidean distance formula. Here's an example implementation:

main.rs
use std::f64;

fn euclidean_distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
    ((x2 - x1).powi(2) + (y2 - y1).powi(2)).sqrt()
}

fn main() {
    let x1 = 1.0;
    let y1 = 2.0;
    let x2 = 4.0;
    let y2 = 6.0;
    let distance = euclidean_distance(x1, y1, x2, y2);

    println!("The distance between ({}, {}) and ({}, {}) is {}", x1, y1, x2, y2, distance);
}
370 chars
16 lines

In this example, we define a function euclidean_distance that takes four parameters: the x and y coordinates of the two points we want to find the distance between. The function calculates the distance using the Euclidean distance formula sqrt((x2 - x1)^2 + (y2 - y1)^2).

In main(), we define the coordinates of the two points and print out the distance between them. Running this code will output:

main.rs
The distance between (1, 2) and (4, 6) is 5.0
46 chars
2 lines

gistlibby LogSnag