Rust with only statements

I'm trying to learn Rust as best as possible for my job. I learned that Rust is a language with a lot of expressions. But is it possible to program in Rust with statements instead of expressions?

Example, instead of:

for i in 0...10 {
}

using this kind of C++ Syntax:

for (int i = 0; i <= 10; i++) {
}

There are definitely different styles of programming Rust. A classic example is how much you use iterators vs loops.

Regarding the for loop syntax that C++ has, no, Rust does not have exactly the same as what C++ does.

This has nothing to do with statements vs. expressions. The for loop in Rust may as well be a statement because it doesn't evaluate to anything useful and in practice is used as a statement, even though technically it's still an expression.

If you are asking whether the syntax of for loops is the same in Rust as it is in C, the no, it's evidently not the case. In general, you shouldn't try to imitate low-level C constructs in Rust.

The for loop is both an expression and a statement (a so called "expression statement").

You're using a very old notation for 0...10 that no longer works. This is now written 0..=10.

The C++ syntax also contains a bunch of expressions: 0, i <= 10, i++ are all expressions.

Note that C++ also uses the concept of "expression statements", for instance std::cout << "hello" is an expression.

You never said what exactly you don't like about the for i in loop syntax. If you wanted to imitate that C++ loop, you could write this:

let mut i = 0;
while i <= 10 {
    i += 1;
}

The rust syntax is normally considered an improvement - and even better if you can avoid referencing the index variable. (_).

But you could use a while loop if you want to hand-crank the index manually

let mut i=0;
while i<=10 {
    i+=1;
}

In fact - if it is a const function that can be evaluated at compile time, you have to write it like this...

Maybe I understand this wrong. According to what I have learned so far, expressions are slower in runtime and have more operations to be translated than statements. And I want to know with my example or my question if it is not possible to use statements instead of expressions.

Are you referring to for i in 0..10 { ... } vs let mut i = 0; while i < 10 { ... i += 1; }? Before optimizations, yes the first is slower due to the first using the iteration abstraction, but after optimizations they produce identical assembly and thus are exactly as fast. And for iterating lists using the for in syntax can even be faster as it allows skipping a bound check. If you mean statements vs expressions in general, no not at all. Most statements are really expressions that discard the result. Something like let foo = 1 + 2; is a statement which contains the expression 1 + 2 and even foo(); is a statement containing the expression foo().

2 Likes

I'm not referring to specific code. I mean the general question of whether expressions are slower than statements or if there is no difference.

I have no idea where you got that from, but for certain it's complete nonsense.

1 Like

I read it once on reddit.

Well that explains a lot. Performance doesn't correlate with whether something is an expression or a statement in the source language. Expression vs. statement is mainly an artificial distinction at the language/type system level, and doesn't say anything about the generated code.

(Not to mention that, as pointed out above, some parts of the code are both expressions and statements at the same time, so based on this assumption, they would be less efficient than themselves, which is impossible.)

6 Likes

That makes sense. Thank you!

I do not understand your question.

In programming languages an expression is something like `(x + y) * ( 2 + f(z)) that evaluates to some value. Expression (computer science) - Wikipedia As such I don't see how we can program anything without expressions.

Meanwhile a statement is something that carries out some action. Be that a declaration, assignment whatever. A statement may well have expressions within it. But it may not.

As such

    f();

is a statement to my mind. Where as

    y = f(x);

Is an assignment statement that evaluates the expression f(x)
(I'm not sure if Rust terminology is exactly that).

Your question is about statements and assignments but your example is about different loop constructs.

To my mind your Rust for loop is a statement but not that you can return values from for loops in Rust which makes them expressions: https://doc.rust-lang.org/rust-by-example/flow_control/loop/return.html

Now, your real question is about the performance of different constructs and how they compare to other languages like C. It's no good going by rumour on reddit or idle speculation. Especially if one is actually doing this for a paid job. There are a couple of ways to satisfy the curious mind:

  1. Put code snippets into "Compiler Explorer" and see what instructions they compile to. One may not now anything much about assembly language but if a lot ore code is generated in some case that is a clue performance is down (not always mind) https://godbolt.org/

  2. Measure it. Create a little benchmark. Educative Answers - Trusted Answers to Developer Questions The criterion crate helps with this.

In general though, I would not worry about comparing Rust performance to C or C++ or fussing over what constructs to use for performance. My experience is that Rust can keep up with C pretty well without my trying. Typically the compiler optimises very well. Better to use concentrate on learning the language and how people generally get things done with it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.