π¦ Day 2 of #100DaysOfRust β Data Types, Functions, Control Flow & Loops
Hey folks! π
Today was an intense and exciting learning day in my Rust journey. I covered a broad range of fundamental concepts:
- Data types (scalar & compound)
- Functions & parameters
- Statements vs expressions
- Control flow using
if
/else
- Looping:
loop
,while
,for
π§ Data Types in Rust
Rust is statically and strongly typed, meaning:
- Type must be known at compile time
- You can't change a variableβs type once declared
- Compiler can infer the type in many cases, but sometimes you need to annotate it
π Scalar Types
A scalar type represents a single value. Rust has four primary scalar types: Integers, Double / Floating Point, Numbers, Booleans, and Characters.
Type | Description | Example |
---|---|---|
i32 , u32 , etc. |
Integers (signed/unsigned, fixed size) | let a: i32 = 10; |
f32 , f64
|
Floating point numbers | let b: f64 = 3.14; |
bool |
Boolean values (true / false ) |
let is_valid = true; |
char |
Single Unicode characters | let emoji = 'π»'; |
π Compound Types
Compound types can group multiple values into one type. Rust has two primitive compound types: tuples and arrays.
π Tuple
A tuple groups values of different types into one fixed-size unit. You create one with a comma-separated list inside parentheses. Each position can have a different type.
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup;
println!("y is: {y}");
π’ Array
An array holds multiple values of the same type and has a fixed size in Rust. You write it using square brackets with comma-separated values.
let months = ["Jan", "Feb", "Mar"];
let scores: [i32; 5] = [1, 2, 3, 4, 5];
let repeated = [3; 5]; // [3, 3, 3, 3, 3]
π§ Functions & Parameters
A way to encapsulate program functionality. Optionally accept data. Optionally return data Utilized for code organization. Also makes code easier to read. Functions are declared using fn and can take parameters and return values.
fn print_sum(a: i32, b: i32) -> i32 {
a + b
}
A function returns a value from its last expression, not statement β no semicolon:
fn five() -> i32 {
5 // <- no semicolon
}
π Statements vs Expressions
Statements are instructions that perform some action and do not return a value.
let x = 5;
Expressions evaluate to a resultant value. and can be assigned.
x + 1 or { let y = 3; y + 1 }
π¬ Comments in Rust
Line comments: // like this
Multi-line: /* comment block */
Doc comments: /// for documentation
π Control Flow with if/else
let number = 6;
if number < 5 {
println!("Small");
} else if number == 5 {
println!("Medium");
} else {
println!("Big");
}
You can even assign with if:
let condition = true;
let number = if condition { 5 } else { 6 };
π Loops in Rust
π loop β Infinite loop
The loop keyword tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop.
loop {
println!("again!");
}
Use break to exit or continue to skip iterations.
π Labeled Loops
When you have loops inside other loops, break and continue normally affect only the loop theyβre directly inside (the innermost one).
But if you want to control an outer loop instead, you can give that loop a label (starting with a single quote like 'myLoop). Then, use break 'myLoop or continue 'myLoop to apply it to that labeled loop.
'outer: loop {
loop {
break 'outer;
}
}
π while loop
A while loop runs code as long as a condition is true. Itβs a cleaner way to write loops that would otherwise need loop, if, else, and break. When the condition is false, the loop stops.
let mut n = 3;
while n != 0 {
println!("{n}!");
n -= 1;
}
π for loop with range
A for loop goes through each item in a collection like an array or vector. It's shorter and easier than using a while loop with an index.
The rev method is used to reverse the order of the items in a collection. It can be used with for loops to iterate over a collection in reverse order.
for num in (1..4).rev() {
println!("{num}!");
}
println!("LIFTOFF!");
You can also loop through arrays:
let a = [10, 20, 30];
for val in a {
println!("Value: {val}");
}
π GitHub
π Day 2 Code on GitHub - https://github.com/Subeshrock/rust-learning-journey/tree/Day002
π¬ Reflection
Rust is strict in all the right ways. The clear distinction between expressions and statements is refreshing. I love how much power match, control flow, and loops give you β without much boilerplate.
Also: itβs impressive how readable Rust is, even when using lower-level constructs!
π§ Up Next (Day 3)
- Pattern matching with match
- Ownership & Borrowing concepts
- Structs and Enums (if time allows)
Thanks for reading and supporting my #100DaysOfRust journey! π
If you're learning Rust too β letβs connect!
Top comments (0)