find the surface area of a cylinder in rust

To find the surface area of a cylinder in Rust, we need to use the formula:

main.rs
SA = 2πr^2 + 2πrh
18 chars
2 lines

where r is the radius of the base of the cylinder, h is the height of the cylinder, and π is the mathematical constant pi.

We can implement this formula in Rust as follows:

main.rs
fn cylinder_surface_area(radius: f64, height: f64) -> f64 {
    const PI: f64 = std::f64::consts::PI;

    let base_area = 2.0 * PI * radius * radius;
    let side_area = 2.0 * PI * radius * height;

    base_area + side_area
}
228 chars
9 lines

This function takes in the radius and height of the cylinder as float64 (double) values, and then calculates the surface area using the formula described above. The resulting surface area is returned as a float64 value.

Note that we define the value of π using the std::f64::consts::PI constant provided by Rust's standard library. This gives us the most accurate value possible for pi using the float64 data type.

gistlibby LogSnag