You have set up a Rust environment on a Ubuntu server and installed Visual Studio Code (VSCode) on your local Windows machine, connecting to the Ubuntu server via the Remote-SSH extension. This guide will walk you through creating a Rust project on Ubuntu and developing and debugging it using VSCode.
Step 1: Create a Rust Project on Ubuntu
- Connect to the Ubuntu server using the Remote-SSH extension in VSCode.
- Open a terminal in VSCode (use
Ctrl + ~or go toTerminal > New Terminal). - Verify that the Rust toolchain is installed and configured:
rustc --version
cargo --version
This should display the versions of the Rust compiler (rustc) and the build tool (cargo).
- Create a new Rust project using Cargo:
cargo new my_rust_app --bin
Here, my_rust_app is the name of the project, and --bin specifies that it's an executable Rust project. For a library, use cargo new my_rust_app --lib.
- Navigate into the project directory:
cd my_rust_app
Step 2: Open the Rust Project in VSCode
- In the Remote-SSH session of VSCode, click
File > Open Folder, and select the project foldermy_rust_app. - VSCode will open the project structure, showing files like
Cargo.tomlandsrc/main.rs.
Step 3: Install Rust Extension (Rust Analyzer)
- In the VSCode extension marketplace, search for and install the Rust Analyzer extension. This extension provides features like smart completion, syntax checking, and more for Rust development.
- After installation, Rust Enalyzer will automatically activate and analyze your Rust project.
Step 4: Write and Run Rust Code
- Open the
src/main.rsfile and modify the initial code to write a simple Rust program, e.g.:
fn main() {
println!("Welcome to Rust programming!");
}
- In the VSCode terminal, execute the following command to build and run the project:
cargo run
You should see the output:
Welcome to Rust programming!
Step 5: Configure VSCode for Debugging
To debug Rust programs in VSCode, you need to install a debugger and configure the debugging environment.
- Install CodeLLDB Extension
- In the VSCode extension marketplace, search for and install the CodeLLDB extension. This extension is the LLDB debugger for Rust.
- Create Debug Configuration
- Press
F5or clickRun > Add Configuration. If prompted, choose Rust (CodeLLDB). - This will create a
.vscode/launch.jsonfile in your project directory with a default configuration similar to:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/target/debug/my_rust_app",
"args": [],
"cwd": "${workspaceFolder}",
"stopOnEntry": false,
"preLaunchTask": "cargo build",
"sourceLanguages": ["rust"]
}
]
}
- Start Debugging
- The
programfield inlaunch.jsonspecifies the path to the executable file generated by the Rust project (target/debug/my_rust_app). - Press
F5to start debugging. VSCode will compile the project and launch it in the LLDB debugger.
Step 6: Set Breakpoints and Debug
- Open the
src/main.rsfile and set breakpoints by clicking on the left margin next to the line numbers where you want to pause execution. - Press
F5to start debugging. The program will run and pause at breakpoints. - While paused, you can use these features:
- Inspect Variables: The debug window shows variables and their values in the current scope.
- Step Through Code: Use toolbar buttons Step Over (F10), Step Into (F11), and Step Out (Shift+F11) to navigate through the code.
- View Call Stack: The debug window displays the call stack, aiding in analyzing the program's execution path.