当前位置: 首页 > 工具软件 > rls-vscode > 使用案例 >

Rust With VSCode

丌官盛
2023-12-01

Rust With VSCode

Rust Debug and Analysis with VSCode

Environments:

[root@localhost ~]# uname -msr
Linux 5.0.9-301.fc30.x86_64 x86_64
[root@localhost ~]# cat /etc/os-release
NAME=Fedora
VERSION="30 (Workstation Edition)"
ID=fedora
VERSION_ID=30
...
VARIANT="Workstation Edition"
VARIANT_ID=workstation

Install Rust and Visual Studio Code

(1) Install Rust

[root@localhost ~]# curl https://sh.rustup.rs -sSf | sh
[root@localhost ~]# source $HOME/.cargo/env
[root@localhost ~]# cargo update
[root@localhost ~]# rustup override set nightly
[root@localhost ~]# cargo install racer

Cargo Enviornment Setting

[root@localhost ~]# cat ~/.bashrc
...
export PATH="$HOME/.cargo/bin:$PATH"
[root@localhost ~]# source ~/.bashrc
[root@localhost ~]# rustc --version
rustc 1.36.0-nightly (a9ec99f42 2019-05-13)
[root@localhost ~]# cargo version
cargo 1.36.0-nightly (759b6161a 2019-05-06)
[root@localhost ~]# vim ~/.cargo/config
[http]
proxy = "host:port"
[root@localhost ~]#  rustup component add rls rust-analysis rust-src clippy-preview rustfmt
[root@localhost ~]# cargo install racer
[root@localhost ~]# dnf install -y lldb python-lldb
[root@localhost ~]# dnf install -y gcc-c++
[root@localhost ~]# export RUST="~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu"
[root@localhost ~]# export CARGO_HOME="~/.cargo/"
[root@localhost ~]# export RUSTBINPATH="~/.cargo/bin"
[root@localhost ~]# export RUST_SRC_PATH="$RUST/lib/rustlib/src/rust/src"
[root@localhost ~]# export PATH=$PATH:$RUSTBINPATH

Setup VS Code

[root@localhost ~]# wget https://go.microsoft.com/fwlink/?LinkID=760867
[root@localhost ~]# dnf install -y code-1.33.1-1554971173.el7.x86_64.rpm
[root@localhost ~]# code --user-data-dir /home/vscode/

Extensions: vscode-rust,
Rust Plugin: Rust(rls);
Rust Debug Plugin: CodeLLDB

How to Create Rust Project

[root@localhost ~]# mkdir -p /home/RustProject
[root@localhost ~]# cd /home/RustProject && cargo new rust_example
[root@localhost rust_example]# tree
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files

Start VS Code

(1) File -> Open Folder -> /home/RustProject/rust_example/
(2) Debug -> Open Configurations -> LLDB, Get launch.json, As below:

    // 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 'vscode_debug_example'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=vscode_debug_example",
                    "--package=vscode_debug_example"
                ],
                "filter": {
                    "name": "vscode_debug_example",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in executable 'vscode_debug_example'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--bin=vscode_debug_example",
                    "--package=vscode_debug_example"
                ],
                "filter": {
                    "name": "vscode_debug_example",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

(3) Debug -> Add Configurations -> Choose {}LLDB: Custom Launch, Get launch.json, As below:

{
    // 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": "custom",
            "name": "Custom launch",
            "targetCreateCommands": [
                # "target create ${workspaceFolder}/<your program>"
                "target create ${workspaceFolder}/target/debug/rust_example"
            ],
            "processCreateCommands": [
                "settings set target.run-args value1 value2 value3",
                "process launch"
            ]
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'vscode_debug_example'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=vscode_debug_example",
                    "--package=vscode_debug_example"
                ],
                "filter": {
                    "name": "vscode_debug_example",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in executable 'vscode_debug_example'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--bin=vscode_debug_example",
                    "--package=vscode_debug_example"
                ],
                "filter": {
                    "name": "vscode_debug_example",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

(4) You must manually change the executable name “program”, as below:

"target create ${workspaceFolder}/<your program>"
                                        ||
                                        \/
 "target create ${workspaceFolder}/target/debug/rust_example"

rust_example is the rust project directory’s name.

(5) Enable Debug breakpoint
File -> Preferences -> Settings -> Features: Debug [Enable]

How to Debug Rust Code

[root@localhost ~]# cd /home/RustProject/rust_example && cargo build
[root@localhost rust_example]# ls
 Cargo.lock   Cargo.toml   src   target
[root@localhost rust_example]# find -name launch.json
./.vscode/launch.json
[root@localhost rust_example]# ls -a
 .   ..  Cargo.lock   Cargo.toml   src   target   .vscode
[root@localhost rust_example]#

Choose Breakpoints

(1) Choose the wanted function ,then Press F9;
(2) Choose the wanted function, Click red point at the head of line;

Start Debugging

Debug -> Start Debugging[F5]

References

  1. How to Debug Rust with Visual Studio Code
  2. Blog post: Using Rust with Visual Studio Code
  3. Rust 环境配置事项一览
  4. Rust 开发环境指北
 类似资料:

相关阅读

相关文章

相关问答