Module 1Variables & TypesLesson 5 of 1024 min
Scalar Types Deep Dive
Integers, floats, bools, chars — choosing types for prices, sizes, and flags.
Integer families
| Type | Range idea | Quant use |
|---|---|---|
i32 |
default signed | days, small counts |
i64 |
large signed | order ids, timestamps (ms) |
u32/u64 |
unsigned | sizes that can't be negative |
isize/usize |
pointer-sized | indices, lengths |
Underscores help readability: 1_000_000.
Floats
f64— default; use for most math (rates, vols, spots)f32— half size, less precision; rarely first choice for risk
Important: binary floats are not decimal money. For display/rounding you manage precision; for true decimal cash some shops use integer cents or decimal crates later.
Bool and char
rust
let is_open = true;
let grade = 'A'; // Unicode scalar — single quotesOverflow behavior
- Debug: overflow often panics
- Release: wraps for primitives
- Prefer
checked_add,saturating_addwhen sizes matter
rust
fn main() {
let q: u32 = 10;
println!("{:?}", q.checked_add(20)); // Some(30)
}Output
Some(30)Example: simple interest
rust
fn simple_interest(p: f64, r: f64, t: f64) -> f64 {
p * r * t
}Exercise
Compute simple interest and future value; print with clear labels.
Challenge
Use checked_add on two u32 day counts (180 + 90) and print the Option.
Tips
- Prefer f64 for market math unless you have a fixed-point standard
- Cast with 'as' carefully — prefer same types in expressions
- u32 for quantities if negatives are invalid
Try it live
Scalar Types Deep DiveRust 2021
Loading editor…
Output
Press Run to compile and execute.