Introduction to Rust Programming with Simple Expressions


In any programming language, expressions are the most basic units of computation - fragments of code that produce a value. Whether you're writing a large system or a small utility, everything ultimately boils down to expressions: numbers being added, values compared, or booleans negated.
In this first text, we'll explore the simplest kinds of expressions in Rust - the kind of operations so fundamental that even a basic calculator can perform them. Think of things like arithmetic, grouping, or comparing values. These are not just the starting point for learning Rust - they are the essential building blocks that any computing device must understand before anything more complex is possible.
Rust, like many functional languages, treats nearly everything as an expression - even control flow constructs like if
or match
. While we’ll see more of that in later sections, our focus here is on the core: literal values, operators, and the basic structure of Rust code that computes values directly.
Understanding how Rust handles these elementary expressions lays the groundwork for everything else to come - from control flow and memory management to systems-level programming and beyond.
The Playground
The easiest and quickest way to try Rust online is through the Rust Playground. The interface lets you enter code on the left and shows the result (whether output or error) on the right when you press “Run.” For every example we discuss, we’ll provide a direct link to the Playground so you can immediately run the code — and freely experiment with it or make your own modifications.
Here is the standard “Hello, world!” program - a tradition in any introductory programming text - written in Rust:
fn main() {
println!("Hello, world!");
}
Click here to open in the Playground.
When you click the link, you’ll see some compilation information on the left, and below it, the output Hello, world!
will be displayed.
For now, you can ignore the outer fn main() { ... }
wrapper - we’ll discuss functions in detail later. What’s important to know is that every Rust program needs a main
function, which serves as the entry point where execution begins. The body of that function contains a single command: println!("Hello, world!")
, which prints the string to the screen.
You might notice the unusual exclamation mark in println!
. That’s not a typo - it has a specific meaning in Rust, which we’ll explain a bit later.
Our first expression that performs a calculation
Now we’re ready to perform a calculation. To see the result, we need to combine the calculation with the println!
command so that the output is printed and visible when we press “Run” in the Playground.
fn main() {
println!("The result is: {}", 22 + 20);
}
You’ll notice that the syntax of println!
has changed slightly - it now takes two arguments: the string "The result is: {}"
and the expression 22 + 20
. The first argument is a format string, and the {}
inside it is a placeholder. That placeholder gets filled with the result of the expression, which in this case is 42
, the sum of 22 + 20
.
{}
placeholders in the format string, but you’ll need to provide a matching number of expressions to fill them in. Additionally, you can write multiple println!
commands - each typically placed on its own line and ending with a semicolon (;
). We leave it to you to experiment with different combinations and observe the results in the Playground.Further Arithmetic Operations
Instead of +
, you can also try other basic arithmetic operations, just like those you learned in elementary school:
-
for subtraction (e.g.,10 - 3
gives7
)*
for multiplication (e.g.,6 * 7
gives42
)/
for division (e.g.,20 / 4
gives5
)%
for remainder (also known as modulo — e.g.,17 % 5
gives2
)
These operations behave as you’d expect when working with whole numbers. Try replacing +
with these in your expressions and see the output.
Special Case of Division and Remainder
However, be careful: if you try to divide by zero or take the remainder (modulo) with zero - for example, 10 / 0
or 10 % 0
- the program won’t print a result. Instead, it will panic.
A panic in Rust is somewhat similar to an exception in other programming languages - although Rust doesn’t use exceptions in the traditional sense. For now, it’s enough to know that a panic causes the program to crash and stop execution immediately. We’ll learn more about panics and error handling later, once we’ve covered a few more fundamentals.
Compound Expressions
We can also build compound calculations by chaining multiple operations. For example, the expression 2 * 10 + 22
first calculates 2 * 10
, and then adds 22
, resulting in 42
. This happens because multiplication has higher precedence than addition, just like in standard arithmetic.
If instead we want to add 2
to the result of 10 + 22
, we need to use parentheses to group the addition:2 * (10 + 22)
- which gives 2 * 32 = 64
.
Using parentheses allows us to control the order in which operations are performed, just like you'd do when solving math problems by hand.
Different Number Types
To understand basic compound expressions, we've been using numbers that look like whole numbers - integers. While we haven’t used them yet, Rust also supports numbers with decimal points - resembling real numbers from mathematics. As you may recall from high school math, both integers and real numbers can be arbitrarily large.
However, computers have finite resources, so numbers in a program must be represented in a limited and specific way. Because of this, Rust provides various numeric types of different sizes, for both integers and floating-point numbers. That level of detail is beyond the scope of this first introduction. But since we’re embarking on a journey into system programming, we’ll soon need to understand how different kinds of numbers are actually represented in the computer - and that’s exactly what we’ll explore in the next text.
Exercises
Add 5 and 9, then multiply the result by 2. [Playground Solution]
Subtract 3 from 20, then take the remainder when divided by 4. [Playground Solution]
Calculate \(6 + 4 \times 3\). [Playground Solution]
Calculate \((3 + 4)(5 + 7)\). [Playground Solution]
Calculate \(\frac{\frac{12 \times 3 + 4}{22 - 5}}{1 + \frac{10}{5}}\). [Playground Solution]
Calculate exercise 1 and 2 in a single program:
Using single
println!
. [Playground Solution]Using two
println!
s. [Playground Solution]
Enjoyed this post?
Subscribe to my newsletter
Read articles from fluid-hacker directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
