From the course: First Look: Rust

Unlock the full course today

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

Return a value from a function

Return a value from a function - Rust Tutorial

From the course: First Look: Rust

Start my 1-month free trial

Return a value from a function

- [Narrator] Functions can return values to the core that calls them. We don't name return values, but we do declare their type after an arrow. In Rust the written value of the function is synonymous with the value of the final expression in the block of the body of a function. Let's create a function with two parameters, both of which are i32 types. Oops, looks like I missed an ln over here on line 13, let me just save that, and let's declare our new function. So use the fn keyword, and the name of the function here is sum, and it takes in two parameters of type i32 which I am naming them x and y here. And the return type would be i32 as well. For now let's go ahead and calculate the sum. So I just add x and y. From the main function let's invoke this function. (keyboard keys clacking) Now since the return type of this function is an i32 we will assign it to a variable. So let a = sum. So here the sum of 10 and five would be calculated and stored in the variable a. Let's go ahead and…

Contents