Rust

[Rust] Getting Started Rust with VS Code

llHoYall 2022. 7. 26. 22:53

Rust is a language empowering everyone to build reliable and efficient software.

Installation

You can install Rust through curl.

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

Please find it in the below link.

https://www.rust-lang.org/tools/install

 

Check the version to verify whether it is properly installed or not.

$ rustc --version

My version was rustc 1.62.1.

cargo

cargo is Rust's build system and package manager.

Projects can be made by cargo new

$ cago new <PROJECT_NAME>
$ cd <PROJECT_NAME>

You can also use cargo init. It will use the current directory.

$ cargo init

cargo build command builds a Rust project.

$ cargo build

We can build and run a project in one step using cargo run.

$ cargo run

And, we can build a project without producing a binary to check for errors using cargo check.

$ cargo check

That's all you need to know about the cargo.

 

Now, we are ready to use Rust!

VS Code Extension for Rust

This extension is officially recommended.

This extension is needed for debugging.

Develop Rust Program using VS Code

Open the project using VS Code.

Make the launch.json file in the .vscode folder.

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "lldb",
      "request": "launch",
      "name": "Debug executable 'hello'",
      "cargo": {
        "args": [
          "build",
          "--bin=hello",
          "--package=hello"
        ],
        "filter": {
          "name": "hello",
          "kind": "bin"
        }
      },
      "args": [],
      "cwd": "${workspaceFolder}"
    },
    {
      "type": "lldb",
      "request": "launch",
      "name": "Debug unit tests in executable 'hello'",
      "cargo": {
        "args": [
          "test",
          "--no-run",
          "--bin=hello",
          "--package=hello"
        ],
        "filter": {
          "name": "hello",
          "kind": "bin"
        }
      },
      "args": [],
      "cwd": "${workspaceFolder}"
    }
  ]
}

Input this code into the file.

Now, open the main.rs file.

You can see the Run | Debug menu.

If you want to run or debug the code, just press the menu.

Conclusion

It is easy and convenient to develop a Rust program using VS Code.

I hope this guide helps you.