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.

Generic struct definitions

Generic struct definitions - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Generic struct definitions

- [Instructor] In my solution to the previous challenge, I defined a struct to represent a rectangle which stores its width and height as 64 bit float values. The code shown here is a simplified version of that solution. Now, what if instead of using floating point values, we want to represent this rectangle using a different data type? Let's try modifying the instantiation values on lines nine and 10 from floats to integers. When I try to run the program, we get a compiler error and this makes sense. Rust is a strictly typed language and we defined the rectangle struct using 64 bit floats but we tried to instantiate it with 32 bit integers. Those are different types so the compiler's going to complain. If from now on, we only plan to define rectangles using integer values, then the straightforward solution would be to change the struct definition. But what if we wanted the flexibility to define structs using either…

Contents