Objective
Understanding Variables and Data Types in Rust Programming
By the end of this lesson, you will understand Rust's syntax for declaring variables, the concept of mutability, and the various data types available in Rust. You'll also learn how to work with strings and perform basic operations.
1. Variables in Rust
In Rust, variables are declared using the let
keyword. By default, variables are immutable, meaning once a value is assigned, it cannot be changed.
Declaring Variables:
let x = 5; // Immutable variable
If you need to change the value of a variable, you can declare it as mutable using the mut
keyword.
Mutable Variables:
let mut y = 10; // Mutable variable
y += 5; // Now y is 15
2. Data Types in Rust
Rust is a statically typed language, meaning that all variables must have a defined type at compile time. The type can be explicitly declared, but Rust often infers the type based on the value assigned.
Common Data Types:
-
Integers:
- Signed integers:
i8
,i16
,i32
,i64
,i128
, andisize
(size dependent on architecture) - Unsigned integers:
u8
,u16
,u32
,u64
,u128
, andusize
let a: i32 = 42; // Signed 32-bit integer let b: u64 = 100; // Unsigned 64-bit integer
- Signed integers:
-
Floating-point Numbers:
f32
(32-bit) andf64
(64-bit)
let pi: f64 = 3.14159; // 64-bit floating point
-
Boolean:
- Represents a value of either
true
orfalse
.
let is_rust_fun: bool = true;
- Represents a value of either
-
Characters:
- Represented by the
char
type, it holds a single Unicode character.
let letter: char = 'R';
- Represented by the
-
Strings:
- Rust has two types of strings: string literals (
&str
) andString
. - String literals are immutable and have a fixed size, while
String
is a growable, heap-allocated data structure.
String Literals:
let greeting: &str = "Hello, Rust!";
String Type:
let mut name = String::from("Alice"); name.push_str(" Smith"); // Appends to the string
- Rust has two types of strings: string literals (
3. Type Inference
Rust’s type inference allows you to omit type annotations in many cases. The compiler will infer the type based on the assigned value.
let x = 10; // Compiler infers x as i32
However, explicit type annotations can be useful for clarity or when the type cannot be inferred.
4. Performing Basic Operations
You can perform arithmetic operations on numeric types in Rust:
let a = 5;
let b = 10;
let sum = a + b; // Addition
let difference = b - a; // Subtraction
let product = a * b; // Multiplication
let quotient = b / a; // Division
let remainder = b % a; // Modulus
For string operations, you can concatenate strings using the +
operator or the format!
macro:
let first_name = String::from("John");
let last_name = String::from("Doe");
let full_name = first_name + " " + &last_name; // Concatenation
let formatted_name = format!("{} {}", first_name, last_name); // Using format!
5. Conclusion
In this lesson, you learned the basics of Rust syntax, focusing on variable declarations, mutability, and data types. You explored common data types and how to perform basic operations with them. Understanding these foundational concepts is essential as you progress in your Rust programming journey.