1. 7
    Rustlings if1: Using an if/else statement to implicitly (or explicitly) return a value
    1m 28s

Rustlings if1: Using an if/else statement to implicitly (or explicitly) return a value

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet

README for this exercise.

Chris Biscardi: [0:01] After variables5, Rust takes us into the land of if statements. If we look at our program, we have a set of test. We'll mostly ignore tests for now, except to mention that they are written in the same file. If you have VS code plugged in, you can actually run the individual test on its own.

[0:24] Note that in our file, we have a function called bigger() that takes an a and a b, both of type i32, and it returns a value of i32. This means that a and b are both 32 integers. This means that both the arguments a and b to our bigger() function are 32-bit integers.

[0:47] The instructions in the comments tell us to complete this function to return the bigger number. We cannot use another function call which, in this case, I'll let you know that there is a max call in the Rust library. We also cannot use additional variables.

[1:04] Let's say if a > b, return a, else return b. Note that this works. Also note that we've used implicit return values because this is the last statement in our function, and thus, these are the last values encountered. We can also be explicit about this and write the return statements explicitly.