Coding/Learn/rust/scalar and compound types
Rust Foundational Course
01.Introduction to Rust & Memory Safety
02.Formatting, Comments & Macros (println!)
03.Variables, Mutability & Shadowing
04.Scalar & Compound Types (Integers, Floats, Tuples, Arrays)
05.Functions, Parameters & Return Expressions
06.Conditionals & If as an Expression
07.Ownership Rules & Move Semantics
08.References & Borrowing (&T)
09.Mutable References (&mut) & Aliasing XOR Mutability
10.Slices & String Types (&str vs String)
11.Loops in Rust (loop, while, for)
12.Pattern Matching with match Expressions
13.Structs, Associated Functions & Methods (impl)
14.Enums & the Option<T> Type (No Null!)
15.Concise Pattern Matching (if let & let else)
16.Vectors & Growable Collections (Vec<T>)
17.Error Handling with Result<T, E> & the ? Operator
18.Traits, Shared Behavior & Generics
19.Closures & Functional Iterators (map, filter, collect)
20.Capstone: High-Performance In-Memory Log Telemetry Engine
Course Certificate0%

Complete all lessons to earn

LearnScalar & Compound Types (Integers, Floats, Tuples, Arrays)
4 / 20 0%
Lesson 04 of 20
6 mins read

Scalar & Compound Types (Integers, Floats, Tuples, Arrays)

PreviousNext Lesson
In Plain English

Rust is statically typed with strong type inference. It features scalar types (integers i8-i128, u8-u128, isize/usize, f32/f64, bool, char) and compound types (tuples and fixed-size arrays [T; N]).

The Mental Model

"Scalar types are individual Lego bricks (single numbers or letters). Compound types are assembled Lego kits—a tuple is a multi-color assorted kit, and an array is a rigid tray of identical bricks."

Deep Dive: How It Works

Integer Types: i32 is the default integer type. usize matches the target CPU pointer architecture.

Tuples: Group multiple values of different types with fixed arity: let t: (i32, f64, &str) = (1, 2.5, "hi");.

Arrays: Fixed-length lists stored contiguously on the stack: let a: [i32; 3] = [10, 20, 30];.

Syntax Blueprint

let tuple: (i32, bool) = (42, true); let arr: [i32; 3] = [1, 2, 3];

Tuples access elements with .index (tuple.0), while arrays access with [index] (arr[0]).

PointCore Rules to Remember

Point star
Type Annotation: let x: i64 = 5000; explicitly defines variable size.
Point star
Tuple Indexing: tuple.0 and tuple.1 extract heterogeneous fields.
Point star
Array Sizing: Arrays have fixed sizes known at compile time ([type; count]).

Common Beginner Traps & How to Fix Them

Mistake:Trying to change the length of a Rust array.

Why it happens: Standard Rust arrays [T; N] have a fixed size on the stack and cannot grow.

How to fix: If you need a growable dynamically-sized collection, use Vec<T> instead.

Pro Tip: Use numeric literal underscores for readability: let one_million = 1_000_000_u32;.

Live Interactive Example

Hit Run Code to see it live
Scalars, Tuples and Arrays
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates primitive types, tuple field access, and array indexing.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Extract tuple and array elements

  • PointDeclare let config: (&str, u16) = ("localhost", 8080);
  • PointDeclare let ports: [u16; 2] = [8080, 8443];
  • PointPrint "Server: localhost on port 8080 | SSL: 8443".
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates