From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Represent shapes

Solution: Represent shapes - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Solution: Represent shapes

(upbeat music) - [Instructor] My solution for the challenge to represent a shape starts by defining a new struct named Rectangle with two fields, width and height, both stored as 64-bit floating point values. It's pretty straightforward. The more interesting part of this solution is in the implementation block starting on line six which contains my methods and associated function. Since get_area on line seven is a method, it takes the reference to the struct itself. It then uses that self-reference to access the values from the width and height fields, and calculate the area of the rectangle, which it then returns as an f64. The next method on line 11 named scale will need to modify the width and height values, and therefore, takes a mutable reference to the struct as its first input argument, along with the scalar value to multiply them by. Lines 12 and 13 then use the arithmetic multiplication and assignment operator…

Contents