Skip to content

Overview

8 min read

Learn the fundamental building blocks of Rust: variables, types, operators, and basic syntax.



By the end of this section, you will:

  • Understand Rust’s immutability-by-default philosophy
  • Know all basic data types and when to use them
  • Use type annotations and type inference
  • Perform arithmetic and logical operations
  • Write clear comments and documentation
  • Format output with println! macro

  • Reading: 60-90 minutes
  • Hands-on Practice: 45-60 minutes
  • Exercises: 30-45 minutes
  • Total: 2.5-3 hours

If you want to jump right in:

  1. Variables (20 min) - Most important!
  2. Types (20 min)
  3. Output (15 min)
  4. Skip to exercises

Then come back for operators and comments.


1. Variables and Mutability (the big difference!)
2. Basic Types (integers, floats, etc.)
3. Operators (arithmetic, comparison)
4. Comments (documentation)
5. Output and Formatting (println!)

All topics are interconnected, sequential reading recommended.


The biggest difference from TypeScript/JavaScript:

TypeScript:

let x = 5; // mutable
x = 6; // OK

Rust:

let x = 5; // immutable by default!
x = 6; // Compile error!
let mut y = 5; // explicitly mutable
y = 6; // OK

Key insight: Rust makes you think about mutability upfront.

TypeScript:

let x: number = 42; // One number type
let y = 3.14; // Also number

Rust:

let x: i32 = 42; // 32-bit signed integer
let y: f64 = 3.14; // 64-bit float
let z: u8 = 255; // 8-bit unsigned integer

Why: More control over memory and performance.

Both languages have it:

let x = 5; // TypeScript infers: number
let x = 5; // Rust infers: i32

But Rust’s inference is more powerful in complex scenarios!

Mostly the same:

// TypeScript
let sum = 5 + 3;
let is_equal = x === y;
// Rust
let sum = 5 + 3;
let is_equal = x == y; // Note: no ===, just ==

Similar syntax:

// TypeScript
// Single line comment
/* Multi-line comment */
/** JSDoc documentation */
// Rust
// Single line comment
/* Multi-line comment */
/// Documentation comment (generates docs!)

Different but powerful:

// TypeScript
console.log(`Hello, ${name}!`);
console.log("x =", x, "y =", y);
// Rust
println!("Hello, {}!", name);
println!("x = {} y = {}", x, y);

AspectTypeScriptRust
DefaultMutable (let)Immutable (let)
Make immutableUse constDefault!
Make mutableDefault!Use let mut
ReassignmentAlways allowedOnly with mut
FeatureTypeScriptRust
Number typesnumberi8, i32, f64
Integer typesnumber12 different types!
Type annotations: Type: Type (same!)
Type inferenceGoodExcellent
Null safetystrict modeNo null at all (Option<T>)
Escape hatchesany (opt-out)unsafe / dyn Any (rare)

TypeScript:

let x = 5;
let x = 10; // Error: cannot redeclare

Rust:

let x = 5;
let x = 10; // OK - this is "shadowing"!

Shadowing is a Rust feature that lets you reuse variable names.

TypeScript:

const MAX_SIZE = 100; // Runtime constant

Rust:

const MAX_SIZE: u32 = 100; // Compile-time constant
// ^^^ Must have type annotation!

Why does Rust do this?

  1. Safety: Prevents accidental modification
  2. Concurrency: Immutable data is thread-safe
  3. Optimization: Compiler can optimize better
  4. Intent: Makes mutability explicit

Coming from JavaScript, this feels backwards at first!

// JavaScript - everything is mutable
let x = 5;
x = 6; // No problem
// Rust - explicit mutability
let x = 5;
// x = 6; // Won't compile
let mut y = 5;
y = 6; // OK

You’ll get used to it! After a week, you’ll appreciate it.

Rust is smart about types:

let x = 5; // Inferred as i32
let y: u8 = 5; // Explicitly u8
let z = 5u8; // Suffix notation

When you need annotations:

// Compiler can't infer
let numbers: Vec<i32> = Vec::new();
// Multiple possible types
let guess: u32 = "42".parse().expect("Not a number!");

Everything in Rust is an expression (almost):

let x = {
let y = 3;
y + 1 // No semicolon - this is the return value!
};
// x is now 4

Compare to TypeScript:

// TypeScript - blocks don't return values
let x = (() => {
let y = 3;
return y + 1;
})();

  • Understand let vs let mut
  • Know the basic integer types (i32, u32, etc.)
  • Use println! with formatting
  • Write simple arithmetic expressions
  • Understand shadowing vs reassignment
  • Know when to use each integer type
  • Use all comparison operators
  • Write documentation comments
  • Use advanced formatting (debug, alignment)

”cannot assign twice to immutable variable”

Section titled “”cannot assign twice to immutable variable””
let x = 5;
x = 6; // Error!

Solution: Add mut:

let mut x = 5;
x = 6; // OK
let x = Vec::new(); // What type?

Solution: Add type annotation:

let x: Vec<i32> = Vec::new(); // OK
let x: u8 = 255;
let y = x + 1; // Compile error: this arithmetic operation will overflow

When both operands are constants, the compiler catches the overflow at compile time (error: this arithmetic operation will overflow). To see the runtime behavior, the overflow has to involve a value the compiler can’t fold away:

let mut x: u8 = 255;
x += 1; // Panics in debug mode; silently wraps to 0 in release mode

In debug builds this panics with attempt to add with overflow; in release builds the check is removed and the value wraps around to 0.

Solution: When you want wraparound, use the explicit wrapping/saturating arithmetic methods (wrapping_add, saturating_add, checked_add) — we’ll cover these later.


These are the building blocks! Everything in Rust builds on:

  • Variables and mutability
  • The type system
  • Basic operations

Master these, and the rest becomes easier.

  • Section 03 (Functions): Use these types as parameters
  • Section 05 (Ownership): Immutability is key to ownership
  • Section 06 (Data Structures): Compose basic types
  • Section 08 (Error Handling): Type system enables Result/Option
ConceptTypeScript SectionRust SectionDifficulty
VariablesDay 1Day 1Medium
TypesDay 1-2Day 1-2Medium
OperatorsDay 1Day 1Easy
MutabilityNot emphasizedCriticalHard
Type safetyOptionalMandatoryMedium

The mutability concept is the hardest part for JS/TS developers.


Let’s start with the most important concept: variables and mutability!

Or jump to: