Skip to content

Using Rust with Visual Studio Code

Visual Studio Code is the most popular and recommended code editor for Rust development.

The experience is excellent, but it requires one key extension to make it work.

How to Set It Up (2 Steps)

After installing RUST as instructed here, (or you can also install after installing these extension):

  1. Install the rust-analyzer Extension:This is the official language server for Rust. It’s what gives you all the smart features.
    • Go to the Extensions tab in VS Code (the icon with the four squares).
    • Search for rust-analyzer.
    • Install the one published by the Rust-lang team.
  2. Install the Debugger Extension:To get a full debugging experience (setting breakpoints, stepping through code), you also need a debugger. If you’re on a Mac/Linux, the best one is CodeLLDB.
    If you’re on Windows, you should use the official Microsoft C/C++ extension: Go to the Extensions tab >> Search for: C/C++ >> Install the one published by: Microsoft. The C/C++ extension pack includes the Microsoft debugger, which rust-analyzer will use automatically to let you set breakpoints and step through your Rust code.

What This Setup Gives You

Once you have rust-analyzer installed, you’ll get all the modern features you’d expect from an IDE, right inside VS Code:

  • Intelligent Code Completion: It autocompletes your code, including methods and types.
  • Real-time Error Checking: It shows you compiler errors as you type, so you don’t even have to run cargo build to see a mistake.
  • “Go to Definition”: You can jump straight to the source code of any function or type.
  • Run & Debug Buttons: Small “Run | Debug” buttons will appear directly above your fn main() and test functions, letting you run or debug your code with a single click.
See also  Using AI to better code, debug and manage projects

You now have a complete, professional-grade Rust development environment set up.


Create and run your very first Rust project in VS Code

We’ll use cargo (the tool you just installed) to create the project, and then use VS Code to write and run the code.

Step 1: Create a New Project with Cargo

First, open your Terminal. cd to a directory where you want to keep your projects (like ~/Documents or ~/Developer).

Then, run this command to create a new project called hello_rust:

Bash

cargo new hello_rust

You’ll see this output:

     Created binary (application) `hello_rust` package

This command just created a new folder named hello_rust with all the necessary files inside, including a simple “Hello, world!” program.

Step 2: Open the Project in VS Code

Now, let’s open that folder in VS Code.

  1. In your Terminal, go into the new directory:
    Bash
    cd hello_rust
  2. Type the following command to open the entire folder in VS Code:
    Bash
    code .
    (The . just means “open the current folder”.)

Step 3: Explore the Project Files

In the VS Code file explorer on the left, you’ll see two main files:

  • Cargo.toml: This is the manifest file for your project. It keeps track of its name, version, and any dependencies (other libraries) you might add later.
  • src/main.rs: This is where all your application code lives. (.rs is the file extension for Rust).

Step 4: Run Your “Hello, world!” Program

Click on the src/main.rs file to open it.

You’ll see the “Hello, world!” code that Cargo created for you:

Rust

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

Now, look just above the fn main() line. Since you installed rust-analyzer, you should see two small, clickable text buttons:

See also  How to debug codes generated by ChatGPT

Run | Debug

Click the “Run” button.


Step 5: See the Output

A Terminal panel will open at the bottom of your VS Code window. Cargo will compile your program and then run it. You will see the output:

Bash

    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/hello_rust`
Hello, world!

That’s it! You’ve successfully created, compiled, and run a Rust program entirely from within VS Code.

From here, you can just change the text inside the println! macro, click “Run” again, and see your changes.

Now, you can continue with the other example here of RUST.

Leave a Reply

error: Content is protected !!