1. 12
    Rustlings functions5: Implicitly returning values from functions
    1m 10s

Rustlings functions5: Implicitly returning values from functions

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

README for this exercise.

Chris Biscardi: [0:00] In functions5, we have our main() function with an answer set to this value of square (3). We use another println! Macro to print out our answer. The square() function takes a number parameter that's an i32 and returns an i32. All we're doing here is squaring the number.

[0:18] The Rust error tells us there's mismatched types, specifically in the square() function that we just took a look at. It points to the return value of i32 where it expected an i32 and found two parentheses. These two parentheses are usually pronounced unit, U-N-I-T. A unit type is basically nothing.

[0:40] What Rust is telling us here is that this function implicitly returns nothing as its body, as its body has no tail or return expression. We have a little bit of help text here that says, "Consider removing this semicolon." Let's try that.

[0:58] Note that this works because the implicit return requires you to not use a semicolon at the end. If we were going to use a semicolon, we could also use an explicit return, and that would work.