Rust Daily Learning - Day 2
Overview
Welcome back to the Rust daily learning series! Today, we will dive into variables, data types, and mutability in Rust. Understanding these fundamentals will help you become proficient in Rust programming.
Variables and Mutability
By default, variables in Rust are immutable, meaning their values cannot be changed once assigned. To create a mutable variable, use the mut
keyword:
fn main() {
let mut x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x is: {}", x);
}
In this example, we create a mutable variable x
, set its value to 5, and then change its value to 6.
Data Types
Rust is a statically-typed language, which means that the data type of a variable must be known at compile time. Rust has two categories of data types: scalar and compound.
Scalar Types
Scalar types represent a single value. There are four primary scalar types in Rust:
- Integers - signed (
i8
,i16
,i32
,i64
,i128
) and unsigned (u8
,u16
,u32
,u64
,u128
). The default integer type isi32
. - Floating-point numbers -
f32
andf64
. The default isf64
. - Booleans -
bool
. The values can betrue
orfalse
. - Characters -
char
. Single Unicode scalar values enclosed in single quotes, e.g.,'A'
,'ß'
,'🤖'
.
Compound Types
Compound types group multiple values into one type. Rust has two compound types:
- Tuples - A fixed-size collection of values with different types. Example:
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {}", y);
}
- Arrays - A collection of values with the same type and fixed length. Example:
fn main() {
let arr: [i32; 5] = [1, 2, 3, 4, 5];
let first = arr[0];
let second = arr[1];
println!("The values are: {} and {}", first, second);
}
That’s all for today! In the next installment, we will learn about control flow in Rust, including conditionals and loops. Stay tuned and keep learning!