Scala Developer Journey into Rust - Part 3: Expression Based Language
Rust is one of the major programming languages that’s been getting popular in recent years. It has many advanced high level language features like Scala.This made me interested to learn Rust. So in this next series of blogs I will share my experience with Rust from a Scala developer point of view. I would like to explore how these two language approach things. I would like to explore the similarities and their differences.
This is third post in the series. In this post, I will be talking about expression based language. You can find all the other posts in the series here.
Expression Based Language
Rust syntax looks very similar to C/C++ languages. But in reality,it’s quite different than these languages. One of the important distinction is statement vs expression.
In a programming language, expression is a block of code which returns a result always. Whereas statement is block of code which doesn’t return anything.It depends upon the side effects for actual work. For ex: In C or Java, if condition is a statement because it doesn’t return any value.
One of the challenges with statement based languages is they don’t compose well. Also writing immutable data based programs in these languages become tricky as they depend heavily on side effect based programming.
Scala and other family of functional languages prefer expressions over statements. In these languages, everything is an expression. All the conditions, loops, block of code returns a value. This makes them highly composable.
Rust follows this suite for system programming. Rust is an expression based language. Every construct of rust return an expression which makes it behave very much like Scala. That’s why you will see similar construct like pattern matching etc. in Rust also.
In the next few sections, we will see how Rust expression based programming compares to the Scala.