Making it work

The final step is to add a main function. We can use the same function as was in the non-generic trait example but with the oval removed:

fn main() 
{
let peri = Shape
{
line_one: 5,
line_two: 12
};
println!("line_one = 5, line_two = 12, Perimeter = {}",
peri.calc ());
}

When compiled, this gives the following output:

As we have created the second implementation, extending the main function to include the second calculation should be trivial.

See 09/generic_trait_full for the code files of this part.

We also need to implement the f32 calculation:

impl Calculate<f32> for Shape<f32> 
{
fn calc(&self) -> f32
{
3.1415927f32 * self.line_one * self.line_two
}
}

When this is compiled, we see the following:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset