The Rust Programming Language

Statements and Expressions Q&A

Richard Feldman

Richard Feldman

Vendr, Inc.
The Rust Programming Language

Check out a free preview of the full The Rust Programming Language course

The "Statements and Expressions Q&A" Lesson is part of the full, The Rust Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

Richard answers student's questions regarding if the last expression is the return, if a function contains more than one expression can it also be automatically returned by the compiler, and if early returns need to be specified. Student's questions regarding if the type is taken into account in comparisons, if variables of conditional statements if they can be passed around in the string interpolation, and if auto returns can be used for nullish conversion are also covered in this segment.


Transcript from the "Statements and Expressions Q&A" Lesson

>> Any questions about statements and expressions and how you can use them for automatic return in functions or if branches like this?
>> The last expression would be the return then it sounds like.
>> Right, yeah. So if I wanted, I could put other stuff in this block above this.

And that would be the same thing as it would be the function where like the, the last expression in there would be automatically become the result of the whole branch.
>> If a function contains more than one expression, then also can we still do this?
>> Great question.

Yeah, if a function contains more than one expression also can you do this. So basically the rule is you can have as many statements as you want in a row, and then after that one final expression. But if you tried to put like, here's an expression, then here's another expression, you get a compiler error.

So it has to be like a bunch of statements followed by an expression at the end that's just sort of hanging out. Then that last expression right there, that's sort of the last thing in the function. That's what gets turned to the automatic return and it's the same thing inside these if blocks.

>> Would you want to specify explicitly a return statement if you were trying to make an early return?
>> Yeah, for sure. Yeah, if you want to do an early return, then you do need to use the return keyword for that. But if you want, you can do an early return and then still at the end have like the default return use the expression style, that's totally fine.

But yeah, if you wanna do an early return, either you need to do that or you need to do something like this where you have conditionals like throughout the function. You never need to use the return keyword, you can always express the same thing without it. But yeah, early returns generally use the return keyword for that.

>> For the comparison, I'm guessing that it takes that type into account like a string of 1.1. Would it be false if it was compared to a float of 1.1?
>> That's even better you would get a type error. So if you tried to compare if the string foo is greater than one, it would just not compile.

>> And then the second question was kind of setting these variables of conditional statements. Can you then like pass these around like in the string interpolation?
>> Sure yeah, if you want to [LAUGH].
>> Yeah that's awesome.
>> Yeah, so actually this entire if like starting from the INF to this close curly brace, that whole thing is an expression.

So you can just pick it up and drop it anywhere you want. [LAUGH]
>> Can that be used for knowledge coercion?
>> Great question. Can that be used for knowledge coercion, RUSS actually does not have a concept of null or undefined or any similar kinds of coercion. We'll see how, like the alternative that it has when we get to EOMS.

But basically those are no nil undefined, anything like that RUSS does not have it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now