DEV Community

Subesh Yadav
Subesh Yadav

Posted on

Day 2 of #100DaysOfRust – Data Types, Functions, Control Flow & Loops in Rust

πŸ¦€ 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}");
Enter fullscreen mode Exit fullscreen mode

🟒 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]
Enter fullscreen mode Exit fullscreen mode

πŸ”§ 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
}
Enter fullscreen mode Exit fullscreen mode

A function returns a value from its last expression, not statement β€” no semicolon:

fn five() -> i32 {
    5  // <- no semicolon
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Statements vs Expressions

Statements are instructions that perform some action and do not return a value.

let x = 5;
Enter fullscreen mode Exit fullscreen mode

Expressions evaluate to a resultant value. and can be assigned.

x + 1 or { let y = 3; y + 1 }
Enter fullscreen mode Exit fullscreen mode

πŸ’¬ Comments in Rust

Line comments: // like this

Multi-line: /* comment block */

Doc comments: /// for documentation
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Control Flow with if/else

let number = 6;

if number < 5 {
    println!("Small");
} else if number == 5 {
    println!("Medium");
} else {
    println!("Big");
}
Enter fullscreen mode Exit fullscreen mode

You can even assign with if:

let condition = true;
let number = if condition { 5 } else { 6 };
Enter fullscreen mode Exit fullscreen mode

πŸ” 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!");
}
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” 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;
}
Enter fullscreen mode Exit fullscreen mode

πŸ” 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!");
Enter fullscreen mode Exit fullscreen mode

You can also loop through arrays:

let a = [10, 20, 30];
for val in a {
    println!("Value: {val}");
}
Enter fullscreen mode Exit fullscreen mode

πŸ”— 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)