educative.io

Last statement should have no semicolon?

In the example of the match expression (method 2), the last statement, statementN, is ended with a semicolon. However, if a statement end with a semicolon () is returned, not the value of the last statement.
So the variable result in method 2 will always be (),

I think it is better, and more clear, if the semicolon of the last statement of each block is removed.


Course: Learn Rust from Scratch - Free Interactive Course
Lesson: Match Expression - Learn Rust from Scratch

Hi @NJ_van_den_Berg !!
In this code, the last statement in the block is the call to println!, which does not return a value. However, since this statement is not being used to compute the value of found_course, it does not affect the correctness of the code.
This code is correct and will work as expected, printing Course name : Rust to the console. However, if you needed to use the value of the last statement in a block, you should make sure that the statement does not end with a semicolon.
I hope it helps. Happy Learning :blush: