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.

Function return values

Function return values - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Function return values

- [Instructor] We saw how to define functions with input parameters to parse data to a function. Now, let's look at how we can use return values to get resulting output data back from a function. To demonstrate that, let's define a new function to square a given input value. For this function to pass a result value back to wherever it was called from, we'll need to specify the data type for that returned value in the function signature. We do that after the input parameters, by typing dash, then a greater than symbol, which together look like an arrow, followed by the data type. This function will return in i32. Notice that we did not have to provide a name for the return value, like we did for the input parameter. For the body of this function, we'll compute the square by multiplying X by itself. I intentionally did not add a semi-colon to the end of line six, which leaves X times X as an expression. In Rust, when…

Contents