Compound Types
Tuples and arrays.
What Are Compound Types?
Compound types group multiple values into one. Rust has two primitive compound types:
- Tuples — fixed-size groups of possibly different types.
- Arrays — fixed-size groups of the same type.
fn main() {
let tup = (1, 2.0, 'c');
let arr = [10, 20, 30];
println!("{} {}", tup.0, arr[0]);
}Creating Tuples
A tuple bundles values inside parentheses. Each element can be a different type.
The tuple's type is the combination of its element types.
fn main() {
let person: (&str, u32, f64) = ("Alice", 30, 5.6);
println!("{} is {} years old", person.0, person.1);
}All lessons in this course
- let and Mutability
- Scalar Types
- Compound Types
- Shadowing