0%

windows下rust安装及vscode下编译调试配置

windows下rust安装及vscode下编译调试配置

rust安装

官网下载rustup-init.exe,如果不想使用默认的安装位置,则在安装前在系统变量里分别添加CARGO_HOME和RUSTUP_HOME,并同时将这两个路径添加到PATH里面,再运行安装程序.

安装好之后,使用rustup component list可以查看默认安装好的组件.

为了方便调试,还可使用rustup component add rust-src安装源码.使用cargo安装racer之前,最好在CARGO_HOME下使用config文件来配置代理:

[http]
proxy = "http://127.0.0.1:1081"

[https]
proxy = "https://127.0.0.1:1081"

racer安装需要切换到nightly:

rustup install nightly
rustup default nightly

然后使用cargo install racer安装racer,安装好之后,再rustup component add rls rust-analysis安装rls.

至此,rust已经可以正常被编译了,你可以使用cargo new 项目名创建项目,然后cargo build编译.

vscode配置rust

在vscode扩展里搜索rust(rls),安装这个扩展,并将配置里的channel改为nightly.并配置一个tasks.json为cargo build:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cargo build",
            "type": "shell", 
            "command": "cargo build", 
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "showReuseMessage": false
           }
        }
    ]
}

launch.json如下配置:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) 启动",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "preLaunchTask": "cargo build",
            "logging": {
                "moduleLoad": false
            }
        }
    ]
}

这样已经可以使用vscode编译运行rust程序.

vscode断点调试rust

在全局配置里面搜索break,将允许在任何文件下断点开启,这样就可以在rust源码中下断点,然后对某一个行代码使用F11步入的时候,会弹出一个无法找到文件的错误警告,将警告中的一长串编码复制下来,在launch.json下添加如下配置:

"sourceFileMap": {
     "/rustc/5c5b8afd80e6fa1d24632153cb2257c686041d41/src": "E:/rust/toolchains/nightly-x86_64-pc-windows-msvc/lib/rustlib/src/rust/src",
            },

这样就可以在调试的时候步入到相应源码,注意的是/rustc/后面这一长串字符会在更新的时候变化,需要相应的更改.

rust示例代码:

fn main() {
    let a = 10;
    println!("Hello, world!{}", a);
}