Rust Functions and Expressions
Apparently Rust is an "expression-oriented" language.
Github: https://github.com/caveofprogramming/rust/tree/main
Expressions
One peculiarity of Rust is that the last line of a code block becomes the value that the code block evaluates to, as long as there’s no semi-colon on the last line.
So for example we can do this:
And x
now has the value 10
.
An expression, of course, means something that boils down to a single value, so the whole code block here becomes an expression.
This is used heavily in functions to supply return values, as we’ll see.
Function Definition
Function definition seems straightforward enough.
This function accepts two values, an integer and a floating-point value, and it has no return value.
The return type is Unit
, defined by two round brackets ()
, which has a similar meaning to void
in many other languages. It indicates nothing will be returned.
One thing I didn’t properly appreciate when I made my last post, is that there aren’t really two kinds of if
in Rust. It’s not that there’s a statement version of if
and an expression version. There’s only the expression version, apparently.
So here, the last line of the function (which does not end in a semi-colon) is the if
statement, and the last lines of the blocks in the if
statement both end in values, again with no semi-colons.
So the if
expressions returns either 0
or i+5
, and the function then returns that.
We can use the return
keyword in Rust; it’s just not usually needed.
This function accepts an integer and returns an integer.
Preview of Rust Ownership Rules
Take a look at the following code.
It creates a mutable string (a kind of string where we can add or remove text) using String::from
.
The function returns a string, which is assigned to an entirely new variable and then printed.
I’ve defined greet as follows:
push_str()
adds some text to the string, and then the string is returned.
This all works.
However, if I attempt the following, the program won’t compile.
You might expect that it would work, because although greet
modifies the string s
, s
is still in scope when we try to print it.
The problem is that the function takes “ownership” of the string, and s
is then no longer valid when we try to print it.
We’ll delve into this in the next post.
Wokery
I’m gaining the impression that many people associated with Rust or the Rust Foundation are woke as hell. This of course means that they spend a lot of time arguing with people and falling out with each other.
Why is that that “inclusive” so often seems to mean “intolerant”? But that’s the world we live in. If you are woke yourself, then no offence intended. I am simply someone who hates politics and would prefer to just get on with whatever I’m doing. I don’t care what your political views are as long as they don’t directly affect me.
Rust advocates also cause some friction by aggressively promoting Rust and demanding that everything be written in Rust. We’ve seen shades of that so often before, notably with Ruby. Eventually, they’ll get older, Rust will find its place, and they’ll calm down.
My hope is that it’s possible to engage in Rust programming without politics coming into it at all. Politics, in my view, should not be in programming. The less politics is involved in anything, the better.
Thanks again for this tutorial. Although somewhat esoteric, it is enjoyable following you on this journey into Rust. It sounds like it has a lot of potential, but its promoters seem to need a bit of growing up to do yet.
Have you heard of the book "Seven Languages in Seven Weeks"? They dive into the languages enough to get some impression of other programming paradigms. I once studied Prolog aeons ago, which I found to be very elegant in its structural approach to First Order Logic on my Masters degree.