find the surface area of a pyramid in rust

To find the surface area of a pyramid, we need to add together the areas of its faces. Assuming we have the length of the base (b), the height of the pyramid (h), and the slant height (s), we can use the following formula to calculate the surface area:

A = b^2 + 2bs

Here is a rust function that takes in the values of b, h, and s and returns the surface area of the pyramid:

main.rs
fn surface_area_of_pyramid(b: f64, h: f64, s: f64) -> f64 {
    let a_base = b * b;
    let a_side = 2.0 * b * s;
    return a_base + a_side;
}
144 chars
6 lines

To test the function, we can pass in some example values:

main.rs
fn main() {
    let b = 5.0;
    let h = 7.0;
    let s = 8.0;
    let surface_area = surface_area_of_pyramid(b, h, s);
    println!("Surface area of pyramid = {}", surface_area);
}
182 chars
8 lines

In this example, the base of the pyramid is 5 units, the height is 7 units, and the slant height is 8 units. The output of the program should be:

main.rs
Surface area of pyramid = 105.0
32 chars
2 lines

Meaning that the surface area of the pyramid is 105 square units.

gistlibby LogSnag