Learn Rust – Getting Started with Rust

Hello and welcome to the first step of our journey, “Learning Rust”. We’re about to explore Rust, a programming language known for its security, efficiency, and speed.
Some people might say it isn’t the best starting point for beginners. But I think it’s good to jump right in! Rust is designed to be safer and more reliable than many other languages, helping us avoid common mistakes from the beginning.
Learning Rust is like learning to ride a bike with training wheels that guides us and keeps us balanced as we step into the world of programming, stopping us from picking up bad habits that some other languages might let us slip into.

So, let’s get ready! Our journey isn’t just about learning a programming language, but also about mastering programming the right way. Together, we’ll explore the wonderful world of Rust!

What is Programming?

Programming is like being a ship’s captain who give clear instructions to your crew so they know what to do and when to do it. If you don’t, your ship might end up going in circles or, worse, hitting an iceberg!

In programming, your computer is your ship, and the instructions you give it are written in a special language that computers understand. This set of instructions is called a program. Just like a captain, you need to make sure your instructions (or your program) are clear and precise, so your computer (or your ship) knows exactly what to do and when to do it.

Are you ready to be a captain and steer your ship into the exciting waters of Rust? Let’s set sail!

Understanding Computer Memory: Stack and Heap

Before we start learning, it’s important to understand two basic parts of programming. Think of your computer’s memory as a big warehouse, divided into two parts: the Stack and the Heap. Both these parts are used for storage, but they work in different ways.

The Stack

The Stack is like a pile of boxes. To add something, you put it on top of the pile. To remove something, you take it from the top. You can’t take a box from the middle of the pile – they have to be removed in the opposite order from how you put them on. This is called “Last In, First Out”, or LIFO.

In Rust, when you create a variable like this:

let x = 5;

The value 5 is stored in a box on the Stack. When the variable x is no longer needed (for example, when we reach the end of a function where x was created), Rust automatically removes the box from the Stack.

The Heap

The Heap is different. It’s like a big pile of boxes where you can put a box or take a box from anywhere in the pile. But, because the boxes aren’t neatly stacked, it’s harder to find the box you’re looking for. So, every box in the Heap has an address, like a house number, so you can find it again.

In Rust, when you create data on the Heap, you do it like this:

let y = Box::new(5);

Here, Box::new(5) is like saying “I need a box in the Heap for the number 5”. Rust will then put 5 in a box, place the box somewhere in the Heap, and give you the address. The variable y doesn’t actually hold the value 5 – it holds the address of the box in the Heap where 5 is stored.

But, there’s a catch. Unlike the Stack, Rust doesn’t automatically clean up the Heap when you’re done with a box. If you’re not careful, you can end up with a Heap full of boxes that you don’t need anymore, which is a problem called a memory leak. But don’t worry – Rust has a feature called the ownership system that helps prevent this. We’ll learn more about the ownership system in a future post.

Getting to Know Rust

Rust is a type of programming language. Just like we use different languages to talk to each other, like English, Spanish, or Mandarin, we use different programming languages to talk to computers. Rust is known for being safe, which means it helps prevent our programs from making common mistakes, and fast, which means it can perform tasks quickly.

Why Choose Rust?

Rust is becoming more popular because it’s designed to catch many common programming errors even before the program is run. This means that programs written in Rust are less likely to crash or behave unexpectedly. It’s also very efficient, which makes it a great choice for tasks that require a lot of computer resources.

Installing Rust

Before we can start programming in Rust, we need to install it on our computer. You can do this by visiting the official Rust website and following the instructions there. Don’t worry if you don’t understand everything right away – just follow the steps, and remember that it’s okay to ask for help https://users.rust-lang.org!

Starting a New Project

Once you’ve installed Rust, you can create a new project by using a tool called Cargo. Cargo is like a helper that can create new Rust projects, download the parts of Rust that your project needs, and even run your project for you.

To create a new project, open your terminal or command prompt (this is a program that lets you type commands directly into your computer) and type:

cargo new my_first_rust_project

This will create a new folder called my_first_rust_project with the basic files you need to start a Rust project.

Your First Rust Program

Now, let’s write our first Rust program. Open the main.rs file in the src directory of your new project and type the following code:

fn main() {
    println!("Hello, Rust!");
}

This program is very simple. The fn main() part is like the start of our recipe – it’s where our program begins. The println!("Hello, Rust!"); part is an instruction that tells the computer to print “Hello, Rust!” to the screen.

You can run your program by typing cargo run in your terminal or command prompt. You should see “Hello, Rust!” printed to your screen.

Understanding Variables and Data Types in Rust

In programming, we often need to store and manipulate data. This is where variables come into play. Think of a variable as a storage box where we can keep data that we might need to use later.

In Rust, we can create a variable using the let keyword. Here’s an example:

fn main() {
    let message = "Hello, Rust!";
    println!("{}", message);
}

In this example, message is a variable that holds the string “Hello, Rust!”. We then print the value of message to the screen.

Rust has several data types, each of which tells Rust what kind of data we’re working with. Some common data types are integers (whole numbers), floating-point numbers (numbers with decimal points), booleans (true or false), and strings (text).

Here’s an example of how to use different data types:

fn main() {
    let my_integer = 10; // integer
    let my_float = 10.5; // floating-point number
    let my_boolean = true; // boolean
    let my_string = "Hello, Rust!"; // string

    println!("{} {} {} {}", my_integer, my_float, my_boolean, my_string);
}

Rust also has something called constants. These are like variables, but once you put something in them, you can’t change it. You declare a constant with the const keyword, and you have to tell Rust what type of data you’re going to put in it. Here’s an example:

const MAX_VALUE: i32 = 100;
const PI: f64 = 3.14159;

fn main() {
    println!("Max value is {} and Pi is approximately {}", MAX_VALUE, PI);
}

In this example, MAX_VALUE and PI are constants. MAX_VALUE is an integer constant with a value of 100, and PI is a floating-point constant with a value of 3.14159.

Remember, variables are mutable (can be changed) by default in Rust, and constants are immutable (cannot be changed). Also, constants must always be annotated with their data type, while with variables, Rust often can infer the type based on the value you assign to it.

Wrapping Up

Congratulations on completing the first step in your Rust journey! Today, we’ve introduced Rust, discussed its benefits, and walked through the process of setting it up and writing your first program. We’ve also touched on the basics of variables and constants, and how computer memory works in Rust.

But this is just the beginning. In our next post, we’ll delve deeper into variables and data types in Rust. We’ll explore integers, floating-point numbers, booleans, and strings, and how to use them effectively in your programs.

Remember, practice is key when learning a new programming language. So, experiment with what you’ve learned today, and try writing your own Rust programs. If you get stuck, don’t worry – the Rust community is very supportive and there are plenty of resources available to help you.

Stay tuned for the next post in our “Learning Rust” series. Happy coding!

Leave a Comment