Building and Running Rust Programs 🎯

beginner
16 min

Building and Running Rust Programs 🎯

Welcome to the Rust Tutorials on CodeYourCraft! In this comprehensive guide, we'll walk you through the process of building and running Rust programs. By the end, you'll have a strong foundation in this powerful, system-level language.

What is Rust? 📝

Rust is a modern, open-source programming language that is designed for performance and productivity. It is syntactically similar to C++, yet it provides memory safety while maintaining high performance.

Installing Rust 💡

Before we dive into building and running Rust programs, let's make sure you have Rust installed on your machine. You can download Rust from the official website.

Creating a New Rust Project ✅

Now that Rust is installed, let's create a new project. Open your terminal and type:

bash
cargo new my_project

Replace "my_project" with the name of your project. This command creates a new Rust project with a default structure.

Exploring the Project Structure 📝

Navigate into your new project directory:

bash
cd my_project

You'll notice a src folder containing a main.rs file. This is where your code will live.

Writing Your First Rust Program 💡

Let's write a simple "Hello, World!" program. Open src/main.rs and replace its content with:

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

This code defines a main function, which is the entry point of every Rust program. The println! macro prints the string "Hello, World!" to the console.

Building and Running Your Program ✅

To build and run your program, use the following command:

bash
cargo run

Cargo is the Rust package manager and build system. It will compile your program and then execute it. You should see "Hello, World!" printed to the console.

Common Rust Types 📝

Understanding basic Rust types is crucial. Here are some of the most common ones:

  • i32: Signed 32-bit integer
  • u32: Unsigned 32-bit integer
  • f32: 32-bit floating-point number
  • f64: 64-bit floating-point number
  • char: Unicode character
  • bool: Boolean value (true or false)

Quiz: What is the type of the variable that can store a signed 32-bit integer in Rust? 💡

Quick Quiz
Question 1 of 1

What is the type of the variable that can store a signed 32-bit integer in Rust?

That's it for this lesson! You now know how to build and run Rust programs, as well as some of the common Rust types. In the next lesson, we'll dive deeper into functions and control flow in Rust.

Stay tuned and happy coding! 🚀