Friday, October 22, 2021

On Rust expressions and statements

 Compared to C and Ruby there is a fundamental difference in the way Rust treats statements and expressions.

Statements do not return a value.  

Expressions return values.

Rust is expression heavy, statement light. Yet from a control aspect…statements are primarily used to create a boundary  and proscribe orderliness of expressions.  It can be a tangle.

Guide to Rust reader.  Remember statements end with semicolons.

Semicolon ending code fragbits are statements.  Yes, expression statements, one of  three statements, yes it ends in semicolons too.  Yet, if your expression statement is just expressions, when ending the block with cruscteans curly bracket claws can skip the semicolon, but that way requires the result type is ().

The other two statements, which are declaration statements , are let statements and item statements and they require semicolons regardless.

Non-semicolon ending code are expressions, generally, see the expression statement bit above and take it for what it is, confusingly crazy to reading Rust and parsing the world as statements and expressions as an easy first pass.  

But there is a joy to in delving into the mystery of logic writ by others.  Especially when we know how much trouble a programmer ( and the body of code of such programmers) can easily fall into with a language like C.  C is a form of beauty. I feel fear in writing C. And what ever comfort level with C, Rust is a languages which codes more safely. I feel less anxious coding in Rust. And what ever code I might produce for use.

Still many more things to learn about a Rust…

For addition information see https://doc.rust-lang.org/reference/statements-and-expressions.html






No comments:

Post a Comment