I’m on my fourth (4th!) attempt to learn the Rust programming language. If that sounds like a lot, that’s because it is. It took me four tries to get the hang of Python, and now I use it (misuse it?) quite often.
Here are some simple Rust programs to give you an idea of what Rust looks like and how to get started, assuming you’ve done some sort of programming on some other language in the past. These were generated using the Qwen3-coder LLM using the Ollama CLI. I’ve checked them for accuracy and repeatability. My prompt was “I’m learning the Rust programming language. I have Rust 1.89.0 stable installed and know how to use some of the functions of the cargo utility. Can you give me some simple example programs to try and explain how they work?” Here’s the response:
1. Hello World (Basic Structure)
fn main() {
println!("Hello, world!");
}
This is the standard “Hello, World!” program. fn main() defines the entry point function, and println! is a macro that prints to the console.
2. Variables and Types
fn main() {
let x = 5; // Immutable variable
let mut y = 10; // Mutable variable
y = 15; // Can change mutable variables
let name: &str = "Alice"; // Explicit type annotation
let age: i32 = 25;
println!("{} is {} years old", name, age);
}
3. Simple Calculator
fn main() {
let a = 10;
let b = 5;
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a as f64 / b as f64; // Casting to f64 for decimal division
println!("Sum: {}", sum);
println!("Difference: {}", difference);
println!("Product: {}", product);
println!("Quotient: {}", quotient);
}
4. Function with Parameters
fn add_numbers(x: i32, y: i32) -> i32 {
x + y
}
fn main() {
let result = add_numbers(5, 3);
println!("The sum is: {}", result);
}
5. Simple Loop Example
fn main() {
// For loop (like Python's range)
for i in 1..=5 {
println!("Number: {}", i);
}
// While loop
let mut counter = 0;
while counter < 3 {
println!("Counter: {}", counter);
counter += 1;
}
}
6. Working with Arrays
fn main() {
let numbers = [1, 2, 3, 4, 5];
println!("First number: {}", numbers[0]);
println!("Array length: {}", numbers.len());
// Iterate through array
for number in numbers.iter() {
println!("Number: {}", number);
}
}
7. Simple Struct Example
struct Person {
name: String,
age: u32,
}
fn main() {
let person = Person {
name: String::from("Bob"),
age: 30,
};
println!("{} is {} years old", person.name, person.age);
}
How to run these:
- Create a new project:
cargo new rust_examples - Replace the contents of
src/main.rswith one of the examples above - Run with:
cargo run
Start with the first few examples and try modifying them to see how Rust behaves! Each example introduces different concepts that build upon each other.
Conclusion
I find Rust just different enough from previous languages that I’ve worked with, such as C or Python to make it a little bit challenging to pick up. But as you can see, there’s a lot it can do for you and it seems terrifically well thought-out, as languages go.
Please let me know what you think in the comments. More information about the Rust programming language can be found at the official Rust web site: