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 - Rust Tutorial
From the course: First Look: Rust
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…
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.
Contents
-
-
-
-
Variables and mutability3m 15s
-
(Locked)
Scalar data types: Integers4m 2s
-
(Locked)
Scalar data types: Floats2m 10s
-
(Locked)
Compound data types: Tuples3m 50s
-
(Locked)
Compound data types: Arrays3m 55s
-
(Locked)
How functions work4m 12s
-
(Locked)
Return a value from a function3m 44s
-
(Locked)
Control flow: if/else6m 9s
-
(Locked)
Control flow: While loops3m 19s
-
(Locked)
Control flow: For loops2m 9s
-
-
-
-
-
-