No history yet

Variables and Data Types

Storing Data with Variables

In programming, we need a way to store information that we can use later. We do this with variables. Think of a variable as a labeled box where you can keep a piece of data. In Rust, you create a variable using the let keyword.

let an_integer: i32 = 5;

Let's break that down:

  • let is the keyword that says, "we are declaring a variable."
  • an_integer is the name we've given our variable.
  • : i32 is a type annotation. It tells the Rust compiler that this variable will hold a 32-bit signed integer. We'll cover what this means in a moment.
  • = assigns the value on the right to the variable on the left.
  • 5 is the value we are storing.

One of Rust's core principles is safety. By default, variables in Rust are immutable, which means once you assign a value to them, you can't change it. If you try, the compiler will stop you.

This might seem restrictive, but it helps you write more predictable and safer code by preventing accidental changes to data.

Of course, sometimes you need to change a variable's value. To do that, you can use the mut keyword, which stands for mutable.

let mut counter: i32 = 0;
counter = 1; // This is okay because 'counter' is mutable.

A Strong and Static Type System

You may have noticed the : i32 in our examples. This is because Rust is a statically typed language. This means that the type of every variable must be known when the code is compiled, long before it's ever run. This helps catch a huge category of bugs early in the development process.

Rust is also strongly typed. This means it won't automatically convert one type of data into another. For example, you can't add an integer to a floating-point number without explicitly telling Rust how to do it. The compiler enforces these rules strictly to prevent unexpected behavior.

Think of it like a kitchen with labeled containers. You have one for 'flour' and another for 'sugar'. A static and strong type system is like a rule that says you must decide what goes in each container beforehand, and you're not allowed to mix them up. It prevents you from accidentally putting salt in your coffee.

The Basic Data Types

Rust comes with a handful of fundamental, or scalar, data types. These represent single values. The four primary scalar types are integers, floating-point numbers, booleans, and characters.

scalar

adjective

A type that represents a single value.

Let's look at each one.

Integers are whole numbers without a fractional component. They come in different sizes and can be signed (can be negative, zero, or positive) or unsigned (can only be zero or positive).

Signed integers start with i (e.g., i8, i32, i64), and unsigned integers start with u (e.g., u8, u32, u64). The number indicates how many bits of space they take up in memory.

let apples: i32 = 5; // A signed 32-bit integer
let score: u64 = 1000; // An unsigned 64-bit integer

Floating-point numbers are numbers with a decimal point. Rust has two floating-point types: f32 and f64, which are 32-bit and 64-bit in size, respectively. f64 is the default because on modern CPUs, it's roughly the same speed as f32 but is capable of more precision.

let pi: f64 = 3.14159; // A 64-bit float

The boolean type, bool, is one of the simplest. It can only have one of two possible values: true or false. Booleans are most often used for control flow, like in if expressions.

let is_active: bool = true;
let is_finished: bool = false;

Finally, the character type, char, is Rust's most primitive alphabetic type. It's important to note that a char represents a single Unicode Scalar Value, which means it can represent a lot more than just ASCII. You specify char literals with single quotes.

let initial: char = 'C';
let emoji: char = '😻';

These basic types are the building blocks you'll use to create more complex data structures in your Rust programs.

Quiz Questions 1/5

What keyword is used to declare a variable in Rust?

Quiz Questions 2/5

By default, variables in Rust are immutable.

Now you have a solid foundation for how Rust handles data. You've seen how to declare variables and learned about the fundamental types that form the basis of all Rust programs.