📦 Rust Data Types
Statically Typed, but Smart!
Rust is a statically typed language—every variable has a type. But thanks to type inference, you don’t always have to write them out!
✨ Scalar Types
Scalar types represent a single value. There are four:
i32
,u64
, etc. — Integers (signed & unsigned)f32
,f64
— Floating-point numbersbool
— true or falsechar
— a single character like'a'
,'🦀'
rust
Loading…
Rust will infer types if you don't specify them (when it makes sense):
rust
Loading…
🧱 Compound Types
Compound types can hold multiple values. Two big ones:
1. Tuples
Group different types together.
rust
Loading…
2. Arrays
Fixed-size lists of same-type values.
rust
Loading…
📌 Arrays have a fixed size. If you need a growable list, you'll want a Vec
(we’ll get there 😉).
🧠 Type Annotations: When & Why?
Sometimes, Rust needs a little help figuring things out:
rust
Loading…
Be explicit when:
- You call
.parse()
- You want clarity in your code
- You’re working with multiple number types
🎯 Summary
- Scalar types:
i32
,f64
,bool
,char
- Compound types:
tuple
,array
- Rust infers most types unless it gets confused
- Use type annotations when needed for clarity or to satisfy the compiler
🦀 Rust is type-safe but not intimidating—it just wants to be clear and correct!