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 - Rust Tutorial
From the course: Rust Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.