Data Types and Operators
Work with numbers, booleans, characters, strings, tuples, and arrays.
Learning objectives
- Identify common Rust scalar types
- Use arithmetic and comparison operators
- Store multiple values in tuples and arrays
Common Types
Rust supports integer types, floating-point types, booleans, characters, strings, tuples, and arrays. Rust often infers the type automatically, but type annotations can improve clarity.
Operators
Rust supports common arithmetic operators such as addition, subtraction, multiplication, division, and remainder. It also supports comparison operators that return true or false.
Tuples and Arrays
A tuple can hold values of different types. An array holds multiple values of the same type with a fixed length.
Example code
fn main() {
let age: u32 = 30;
let price: f64 = 19.99;
let active: bool = true;
let grade: char = 'A';
let profile = ("Alice", age, active);
let scores = [80, 85, 90];
println!("Price: {}", price);
println!("Grade: {}", grade);
println!("Name: {}", profile.0);
println!("First score: {}", scores[0]);
}
Practice task: Create an array of five marks and print the first, third, and fifth values.