Rust 101: Write, Build, and Debug Your First Program
Tags: rust, beginners, programming, tutorial
Published: 2023-06-09 01:46:47

## Step 1: Installing Rust

Rust can be installed from the official website using rustup, a command-line tool for managing Rust versions and associated tools.

Here's how you can install Rust on various operating systems:

**On Unix-based systems (like Linux and macOS):**

Open a terminal and enter the following command:

```
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

**On Windows:**

Download and run rustup-init.exe from the rustup website. Then, follow the onscreen instructions.

Rustup will install the latest stable version of Rust. The installation also includes cargo, Rust's package manager, and other essential tools.

## Step 2: Verifying the Installation

You can verify your installation by reopening your terminal (or command prompt on Windows) and running:

```
rustc --version
```

You should see the Rust version number, commit hash, and commit date.

## Step 3: Writing the "Hello, World!" Program

Create a new directory for your Rust program, navigate into it, and then create a new file called main.rs.

```
mkdir hello_world
cd hello_world
touch main.rs
```

Then, open main.rs in your preferred text editor and enter the following code:

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

This program defines a function named main that has no parameters and returns no data. main is a special function in Rust: it's always the first code that runs in every executable Rust program. The println! macro prints the string Hello, World! to the screen.

## Step 4: Compiling and Running the Program

You can compile your Rust program using rustc, the Rust compiler, and then run it:

```
rustc main.rs
./main
```

When you run the rustc command, the Rust compiler reads your program, translates it to instructions that your computer understands, and outputs it as an executable binary. The ./main command runs the generated executable. You should see Hello, World! printed to your terminal.

## Step 5: Building and Running with Cargo

Rust's package manager, cargo, also provides functionality to build your project. To use cargo, first, create a new project:

```
cargo new hello_world_cargo
cd hello_world_cargo
```

This creates a new directory called hello_world_cargo, initializes a new Git repository, and generates a simple 'Hello, World!' program.

Open src/main.rs in the hello_world_cargo directory. You should see the 'Hello, World!' program.

You can build and run the program using cargo:

```
cargo run
```

This command compiles your program and runs it in one step.

To compile the program without running it, you can use:

```
cargo build
```

This will create an executable file in target/debug/hello_world_cargo that you can run.

To compile the program for release (with optimizations), you can use:

```
cargo build --release
```

This will create an optimized executable file in target/release/hello_world_cargo that you can run.

You've installed Rust, written a simple program, and learned how to compile and run it both directly and using cargo, Rust's package manager. Let's go one step further!

## Step 6: Debugging the Program

Debugging is a crucial part of developing software, and Rust provides tools to help with this process.

For a simple program like 'Hello, World!', there's not much to debug. However, it's still useful to know how to debug a Rust program.

On Unix-like systems, you can use a debugger like gdb or lldb. Rust programs are compatible with these debuggers.

For example, to debug your 'Hello, World!' program with gdb, you would do the following:

First, compile your program with debug information:

```
rustc -g main.rs
```

Then, run the program under the debugger:

```
gdb ./main
```

Once you're in gdb, you can use commands like run to start your program, break to set breakpoints, and continue to continue running the program after hitting a breakpoint.

For Windows users, you can use the debugger that comes with Microsoft Visual Studio. The process is a bit more graphical and outside the scope of a simple command line tutorial.

## Step 7: Running the Final Build

When you're ready to compile your program for a final release, you can use cargo build --release as previously explained. This command tells cargo to optimize your program, resulting in a slower compile time but faster execution time.

The release build of your 'Hello, World!' program will be located in target/release/.

To run the release build, navigate to the target/release/ directory and run the program:

```
cd target/release/
./hello_world_cargo
```

You should see Hello, World! printed to your terminal. This is the final, optimized build of your program, ready to be distributed.

I hope this guide has helped you understand how to get started with Rust, from installation to running a final build!

If you enjoy my technology-focused articles and insights and wish to support my work, feel free to visit my Ko-fi page at https://ko-fi.com/philipjohnbasile. Every coffee you buy me helps keep the tech wisdom flowing and allows me to continue sharing valuable content with our community. Your support is greatly appreciated!