Menu Close

Articles

Rust: Selection

Selection statements are specific to structured programming. They can be used to create multiple program branches that can be executed or not depending on results of logical expressions or control variables. 

Conditional

Conditional statement is based on if ... else ...  keywords. You can create two program branches. When a condition is true then first branch is executed. When the condition is false the second branch is executed. The second branch is optional but the first is mandatory.

Example:

Notes:

  • condition is not enclosed in parenthesis,
  • second block is never executed, this is called dead code. 

homework: open this example, add parenthesis to condition then run it: decision

Expression “if”

In many languages the decision is only a statement. In Rust the if  is actually an expression that can have a result. The result is given by the value of the last expression in the block. This is specific to Rust and looks a little bit strange for programmer. Sometimes may be useful to think like this especially when there is no ternary operator like “?” used in other languages.

Example:

Note:

  • Expression 5  and 6 are enclosed in a small blocks { 5 }, {6}
  • Boolean type can be created using type bool,
  • This program will always print 6 except if you modify it.

Homework: open this example and change initial value of condition to false: if expression 

Ladder 

In Python we use elsif keyword and in Julia we use elseif keyword to create a multi-deck decision. Here in Rust we do not have a such keywords. Instead you can use else if  that are two keywords. This is possible in Rust due to lack of any symbol required after the “else”. In Python after else you must use “:” but in Rust you can use a block { … } or another if statement to for a multi-path decision block.

I have made this example for you to open and investigate: ladder

Match

There is one more control statement that is very important in Rust. This will replace “switch” or “case” that is available in Julia and Level. The idea of this statement is simple. We create a structure to check several conditions. We execute one statement for witch the condition is true. The beauty of Rust is that this condition is verified such way that all cases are covered. If we do not cover all cases a compiler error will be generated. “match” is one of the most powerful things in Rust.

Example:

Notes:

  1. The match has a special syntax symbol: “=>” this is like a “result” and is not used in any other place in the language. The function result type has a similar symbol “->”. I have no idea why the symbol is different. It could be the same in my opinion.
  2. The symbol “::” is used here to extract element members from Coin. In my opinion we could use “.”. For some reason I do not like this symbol “::”. In Level language we use “.” for all membership operations.
  3. The last element into enumeration has a coma after it. “Quarter,”. In my opinion this is not necessary. I have look twice in the manual and this is the syntax. I can’t complain but looks bad.

Homework: This example is on repl.it. Open and run it: penny

Read next: Repetition