Scalar & Compound Types
Rust's scalar types hold single values. Compound types - tuples and fixed-size arrays - group values with statically known shapes.
Busca en todas las páginas de la documentación
Rust's scalar types hold single values. Compound types - tuples and fixed-size arrays - group values with statically known shapes.
fn main() {
let count: u32 = 42;
let ratio: f64 = 3.14;
let ok: bool = true;
let ch: char = '✓';
let pair: (u32, &str) = (count, "items");
let nums: [i32; 3] = [1, 2, 3];
println!("{count} {ratio} {ok} {ch} {:?} {:?}", pair, nums);
}When to reach for this: Choosing the right primitive or small aggregate before reaching for Vec or custom structs.
fn stats(values: [f64; 4]) -> (f64, f64) {
let sum: f64 = values.iter().sum();
let avg = sum / values.len() as f64;
(sum, avg)
}
fn main() {
let readings = [98.6, 99.1, 98.4, 99.0];
let (total, average) = stats(readings);
println!("sum={total:.2}, avg={average:.2}");
}What this demonstrates:
f64s on the stack)as f64 for explicit numeric cast| Type | Size (typical) | Notes |
|---|---|---|
i32 / u32 | 4 bytes | Default integer type |
i64 / u64 | 8 bytes | Timestamps, IDs |
f32 / f64 | 4 / 8 bytes | f64 is default float |
bool | 1 byte | Only true or false |
char | 4 bytes | Unicode scalar, not ASCII-only |
let hex = 0xff_u8;
let bin = 0b1010_0110;
let wrapped = 255u8.wrapping_add(1); // 0In debug builds, overflow panics. Use wrapping_*, checked_*, or saturating_* when overflow behavior matters.
let arr: [i32; 3] = [1, 2, 3];
let slice: &[i32] = &arr[1..];Arrays have length in the type. Slices are fat pointers (pointer + length) and can refer to arrays, Vec, or stack data.
let t = (1, "a", true);
let (a, b, c) = t;
assert_eq!(t.0, a);Empty tuple () is the unit type - return type of functions that return nothing meaningful.
char is one byte - char is UTF-32. Fix: Use u8 for raw bytes; iterate str with .chars()..get(i) for optional access.let x: i32 = 1i16; needs explicit cast. Fix: Use as or TryInto.0.1 + 0.2 == 0.3 is false. Fix: Compare with epsilon or use integers (cents).[u8; 1_000_000] can overflow the stack. Fix: Use Vec or Box<[T]> on the heap.| Alternative | Use When | Don't Use When |
|---|---|---|
| Struct with named fields | Tuple has more than 2-3 fields or unclear meaning | One-off (x, y) coordinates |
Vec<T> | Runtime-sized collection | Length is fixed and small |
| Newtype wrapper | Distinct units (Meters, Seconds) | Simple internal math only |
SmallVec / array | Usually small, occasionally large | Always heap-allocated data |
i32 when inference has no other constraint. Use explicit suffixes (42u64) in APIs that need a specific width.
Yes, with const generics: struct Buffer<const N: usize> { data: [u8; N] }.
let a = [0u8; 1024]; repeats 0u8 1024 times. The element must implement Copy.
No. bool is 1 byte for alignment and simple addressing.
Only for specific arities via standard impls (e.g. Clone for small tuples). Prefer structs for public APIs.
Pointer-sized signed integer. Use for indexing and len() results (usize).
"42".parse::<i32>() returns Result. See the strings section for FromStr.
No. Use Option<T> for absent values.
Yes, if T: PartialEq. [1,2] == [1,2] works element-wise.
GPU/math kernels where memory bandwidth matters. Default to f64 elsewhere.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 16 jul 2026