Getting Started with Rust – Variables, Functions and Loops
With the tools installed it is time to start learning some language basics:
- Variables
- Functions
- Control structures (if and loops)
The above are covered in the first three chapters of The Rust Programming Language.
Installing the Tools (Update)
Installation of the tools went pretty smoothly and took only a few hours. The Rust in Visual Studio Code page proved to be a nice addition to the links in the blog post.
The page provides information on:
- Intellisense
- Linting
- Refactoring
- Debugging
plus more.
The Rust Programming Language (Chapters 1 through 3)
The initial chapters of the The Rust Programming Language covers the basics of Rust:
- Variables
- Immutability and mutability
- Functions
- Control flow
It was interesting to discover that Rust has a greater degree of distinction between expressions and statements:
Function bodies are made up of a series of statements optionally ending in an expression. So far, the functions we’ve covered haven’t included an ending expression, but you have seen an expression as part of a statement. Because Rust is an expression-based language, this is an important distinction to understand. Other languages don’t have the same distinctions, so let’s look at what statements and expressions are and how their differences affect the bodies of functions.
This difference means that there is no analogy to the following C code:
int x, y; x = y = 1024;
The basic rule is statements perform actions and expressions return a result. So functions that return a result must return an expression. The general rule being that a statement also ends with a semicolon and an expression does not need one.
Now consider this simple (and admittedly contrived example):
fn add_or_multiply(value : i32) -> i32 { if value > 5 { return value * 2; } // Maybe do some other stuff... value + 1 } fn main() { for number in 0..10 { let result = add_or_multiply(number); println!("Number: {number}, Result: {result}"); } }
Running the above generates the following output:
Running `target/debug/hello_world` Number: 0, Result: 1 Number: 1, Result: 2 Number: 2, Result: 3 Number: 3, Result: 4 Number: 4, Result: 5 Number: 5, Result: 6 Number: 6, Result: 12 Number: 7, Result: 14 Number: 8, Result: 16 Number: 9, Result: 18
The line return value * 2; can be changed to:
return value * 2
Note the semicolon has been removed. Running the resultant application also generates the same output. Further investigation is required to determine why this works and also what is considered best practice amongst the Rust community.
Language Highlights
From a C/C++ programmers perspective, two Rust constructs are appealing due to their convenience and ability to make code tidier:
- Using if statements in assignments
- Labelled loops
The first construct is alien to the C/C++ developer but should be familiar to Python developers. This is the ability to use an if statement in an assignment operation:
let x = if y <= MAXIMUM { MAXIMUM } else { y };
This means the trivial function add_or_multiply in the above application could have been written as:
const MAXIMUM: i32 = 5; fn add_or_multiply(value : i32) -> i32 { let result = if value > MAXIMUM { value * 2 } else { value + 1 }; result }
Nice little feature that can make code more compact and readable.
The second nice feature is the ability to label loops. This allows the application to break in an inner loop to also break an outer loop.
'outer_loop: loop { // Setup for the inner loop... loop { if remaining == MAXIMUM { break; } if (count % 2) == 0 { break 'outer_loop; // Ignore even numbers. } // More inner loop processing... } // More outer loop processing... }
The inner loop may be a contrived version of a while loop but it serves to illustrate the language feature. The break ‘outer_loop allows the inner loop to skip even numbers without the need run unnecessary nested if statements.
Conclusion
A slow start but some interesting language features:
- Immutability by default
- Using if statements in assignments
- Labelled loops
Next up is ownership.
Tags: Rust, Software Development
Wednesday, October 15th, 2025 at 8:25 am • Rust, Software Development • RSS 2.0 feed • leave a response or trackback