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 function definitions

Generic function definitions - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Generic function definitions

- [Presenter] We've seen generic data types used to define methods. So it shouldn't be a big surprise to learn that generics can also be used when defining functions. To demonstrate that let's implement a function that returns the biggest of two given input values. This function will have one generic type named T and then two input parameters named a and b which are both of that same type T. And then the return type will also be T. This syntax is similar to what we saw earlier when implementing methods first track using generics. Within this function, we'll use an if else expression to return a or b depending on which one is greater. If a is greater than b return a else return b. Then down in the main function, we'll create a print statement. And we'll call our get biggest function to compare two values. For simplicity, let's compare one and two. Now, if I try to run this program, we get a compiler error. It says…

Contents