Rust Primitive Types, Functions, Control Flow, and Placeholders

Integer Types

Rust provides signed and unsigned integers in various bit widths:

Bit Width   Signed   Unsigned
8-bit       i8       u8
16-bit      i16      u16
32-bit      i32      u32
64-bit      i64      u64
128-bit     i128     u128
arch-dependent  isize    usize

Example declarations:

let val_i8: i8 = 1;
let val_u8: u8 = 1;
let val_i16: i16 = 1;
let val_u16: u16 = 1;
let val_i32: i32 = 1;
let val_u32: u32 = 1;
let val_i64: i64 = 1;
let val_u64: u64 = 1;
let val_i128: i128 = 1;
let val_u128: u128 = 1;
let val_isize: isize = 1;
let val_usize: usize = 1;

Floating-Point Types

Rust has two floating-point types based on IEEE 754:

Bit Width   Type
32-bit      f32
64-bit      f64

Example usage:

let a: f32 = -1.1;
let b: f32 = 1.1;
let c: f64 = -2.2;
let d: f64 = 2.2;

Boolean Type

The bool type represents logical values:

let active = true;

Character Type

The char type represents a Unicode scalar value (4 bytes):

let letter = 'α';

Tuple Type

Tuples group multiple values of potential diffferent types into a single compound type with fixed length:

let triple: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = triple; // destructuring
let first = triple.0;   // accessing by index
let second = triple.1;
let third = triple.2;

Array Type

Arrays are fixed-size collections stored on the stack:

let inferred = [1, 2, 3, 4, 5];
let explicit: [u32; 5] = [6, 7, 8, 9, 0];
let repeated_chars = ['c'; 3]; // ['c', 'c', 'c']
let repeated_nums = [3; 2];    // [3, 3]
let element = explicit[0];

Functions

Function parameters must have explicitly declared types. The final expression in a function body (without a semicolon) is the return value.

fn display(value: u32) {
    println!("{}", value);
}

fn add_with_return(a: u32, b: u32) -> u32 {
    return a + b;
}

fn add_implicit(a: u32, b: u32) -> u32 {
    a + b // no semicolon
}

Associated Functions

Associated functions are called using the :: syntax and are defined within an implementation block for a type:

let mut input = String::new();

Here, new is an associated function of the String type.

Placeholders in Formatting

The {} placeholder is used in formatting macros like println!:

let x = 5;
let y = 10;
println!("x = {} and y = {}", x, y);

Control Flow

if Expressions

Conditions do not require parentheses. if is an expression that returns a value:

let num: u32 = 10;
if num < 5 {
    // ...
} else if num < 10 {
    // ...
} else {
    // ...
}

loop

An infinite loop that can be exited with break. It can also return a value:

let mut counter = 0;
loop {
    counter += 1;
    if counter % 2 == 0 {
        continue;
    }
    if counter > 10 {
        break;
    }
    println!("{}", counter);
}

let result = loop {
    counter += 1;
    if counter > 20 {
        break counter * 2;
    }
};
println!("{}", result);

while

Loop while a condition is true:

let mut total = 0;
let mut i = 1;
while i <= 100 {
    total += i;
    i += 1;
}
println!("{}", total);

for

Iterate over collections or ranges:

let values = [10, 20, 30, 40, 50];
for v in values.iter() {
    println!("{}", v);
}

for n in 0..4 {
    println!("{}", n); // prints 0, 1, 2, 3
}

for n in (0..4).rev() {
    println!("{}", n); // prints 3, 2, 1, 0
}

Tags: rust primitive-types control-flow functions associated-functions

Posted on Wed, 19 Aug 2026 16:35:14 +0000 by bal bal