Variables and Data Types
In this lesson, we'll explore how Rust handles variables and its basic data types. You'll learn about variable declaration, mutability, and the most common data types you'll use in your Rust programs.
Variables and Mutability
In Rust, variables are immutable by default - a key feature that promotes safer code:
fn main() {
let x = 5; // Immutable by default
println!("The value of x is: {}", x);
// This would cause a compile error:
// x = 6; // Cannot assign twice to immutable variable
// To make a variable mutable, use 'mut':
let mut y = 5;
println!("The value of y is: {}", y);
y = 6; // This works!
println!("The value of y is now: {}", y);
}
Basic Data Types
Rust is a statically typed language, which means it must know the types of all variables at compile time. The compiler can usually infer the type based on how we use the variable.
Scalar Types
Integers
fn main() {
let a: i32 = 42; // 32-bit signed integer
let b: u32 = 42; // 32-bit unsigned integer
let c = 42_i64; // 64-bit signed integer
let d = 42_u64; // 64-bit unsigned integer
println!("Different integer types: {}, {}, {}, {}", a, b, c, d);
}
Floating-Point Numbers
fn main() {
let x = 2.0; // f64 (default)
let y: f32 = 3.0; // f32
println!("Floating point numbers: {}, {}", x, y);
}
Boolean
fn main() {
let t = true;
let f: bool = false;
println!("Boolean values: {}, {}", t, f);
}
Character
fn main() {
let c = 'z';
let z: char = 'ℤ';
let heart_eyed_cat = '😻';
println!("Characters: {}, {}, {}", c, z, heart_eyed_cat);
}
Type Conversion
Rust requires explicit type conversion in most cases:
fn main() {
let x = 42_i32;
// Convert i32 to f64
let y = x as f64;
// String to number conversion
let str_num = "42";
let parsed_num = str_num.parse::<i32>().unwrap();
println!("Original: {}", x);
println!("Converted to float: {}", y);
println!("Parsed from string: {}", parsed_num);
// Try changing these values and conversions!
}
Practice Exercises
Try these exercises in the playground above:
- Create variables of different numeric types and perform some basic arithmetic
- Try converting between different numeric types using the
as
keyword - Create a string from a number using the
to_string()
method - Parse a string containing a floating-point number into an
f64
Remember: Rust's type system is one of its strongest features, helping catch potential errors at compile time rather than runtime!