Table of contents
Welcome to the world of Rust, where safety, performance, and a robust type system come together to create a language that's both powerful and enjoyable to use. In this beginner-friendly blog post, we'll demystify the concepts of variable declaration and data types in Rust, providing you with a solid foundation to kickstart your programming journey.
Variable Declaration in Rust
In Rust, variables are placeholders for storing data that can change during the program's execution. Before using a variable, you need to declare it with the let keyword, which tells the compiler that you want to reserve memory for a particular value.
Here's the general syntax for declaring a variable:
let variable_name = value;
Data Types in Rust
Rust is a statically-typed language, which means that the data type of a variable is known at compile-time. The Rust compiler uses type inference to automatically determine the data type of a variable based on the assigned value. However, you can also explicitly specify the data type if needed.
We’ll look at two data type subsets: scalar and compound. In this lesson, we will look into the scaler datatypes.
So usually the compiler can guess. But sometimes you need to tell it, for two reasons:
You are doing something very complex and the compiler doesn't know the type you want.
You want a different type (for example, you want an i128, not an i32).
To specify a type, add a colon after the variable name.
let variable_name: DataType = value;
Let's explore some common data types in Rust:
Integers
Integers represent whole numbers without a fractional component. Rust provides signed and unsigned integers with various bit sizes:
i8
andu8
represents 8-bit signed and unsigned integers, respectively.i16
andu16
represents 16-bit signed and unsigned integers, respectively.i32
andu32
represents 32-bit signed and unsigned integers, respectively.i64
andu64
represents 64-bit signed and unsigned integers, respectively.i128
andu128
represents 128-bit signed and unsigned integers, respectively.
Floating-Point Numbers
Floating-point numbers represent numbers with a fractional component. Rust has two main floating-point types:
f32
: 32-bit single-precision floating-point number.f64
: 64-bit double-precision floating-point number (default for most numeric operations).
Boolean
The Boolean type has two possible values: true
and false
. It's used for logical operations and control flow.
Characters
The char
type represents a single Unicode character and is denoted by single quotes, e.g., 'A'.
Example Code
Let's see some examples of variable declaration and data types in Rust:
fn main() {
// Integer variables
let age = 25; // implecit Type Declaration
let distance: u32 = 42; // Explicit Type Declaration
// Floating-point variable
let pi = 3.14159;
let pie: f32 = 3.14159; // Explicit Type Declaration
// Boolean variable
let is_rust_fun = true;
// Character variable
let grade = 'A';
let space = ' '; // A space inside ' ' is also a char
let other_language_char = 'Ꮔ'; // Thanks to Unicode, other languages like Cherokee display just fine too
let cat_face = '😺'; // Emojis are chars too
}
Print Variables
fn main() {
let age = 25; // Integer variables
println!("Hello, world number {}!", age);
}
println!
is a macro that prints to the console. A macro is like a function that writes code for you. Macros have a !
after them. We will learn about making macros later. For now, remember that !
means that it is a macro.
The {}
in println!
means put the variable inside here. This prints Hello, world number 8!
.
We can put more in, just like we did before:
fn main() {
let height = 4; // Integer variables
let width = 5.2 // Float variable
println!("Hello, box height is {} and width is {}!", height, width);
}
Above code prints Hello, box height is 4 and my width is 5.2!
.
Congratulations! You've learned the fundamentals of variable declaration and common data types in Rust. These concepts form the building blocks of Rust programming and will set you on the path to creating powerful and reliable applications. As you continue your Rust journey, don't hesitate to explore more advanced data types and the language's unique ownership model. Happy coding!