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 3 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.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today