专业的编程技术博客社区

网站首页 > 博客文章 正文

为 Windows 10和VS Code安装 Rust

baijin 2024-09-04 02:12:10 博客文章 5 ℃ 0 评论

更多互联网精彩资讯、工作效率提升关注【飞鱼在浪屿】(日更新)


从全新的 Rust 安装到 Windows 10 上。

  • 首先,安装 rustup。只需访问 rustup 网站即可找到它。https://win.rustup.rs/x86_64,下载rust-init.exe安装程序。
  • 使用 rustup,至少安装一个 Rust 工具链。可以在nightly或stable版本之间进行选择,也可以针对 Windows 或 GNU libc 进行构建。
$ rustup install nightly-msvc

或者如果更精确,像这样:

$ rustup install nightly-x86_64-pc-windows-msvc

特定目标和工具链可能因你的机器而异。

  • 然后你设置默认工具链
$ rustup default nightly-msvc

可以同时使用gnumsvc版本并使用 rustup 切换它们。

自动更新 rustup 工具链

$ rustup self update
$ rustup update nightly //or stable

安装开发工具

有很多工具可以让 Rust 的开发变得更容易,你可以用 cargo 安装它们:

  • Racer:“为编辑器和 IDE提供Rust 代码补全的实用程序”
$ cargo install racer
  • rustsym:“一种Rust代码中查询符号以用于 IDE 的工具”
$ cargo install rustsym
  • rustfmt:“根据样式指南格式化 Rust 代码的工具”
$ cargo install rustfmt
  • ripgrep:“ripgrep 结合了 Silver Searcher 的能力和 grep 的原始速度”
$ cargo install ripgrep
  • 还可以安装rust文档
$ rustup component add rust-doc
  • 最后但并非最不重要的一点是,Visual Studio Code 插件所需的大部分功能都由Rust Language Server 提供,所以继续安装它:
$ rustup self update
$ rustup update nightly
$ rustup component add rls --toolchain nightly
$ rustup component add rust-analysis --toolchain nightly
$ rustup component add rust-src --toolchain nightly

现在我们已准备好继续安装和设置 Visual Studio Code。


VSCode 设置

假设还没有安装VSCode,以下是安装它的步骤:

  • 从https://code.visualstudio.com/获取安装程序并运行它。
  • 为 VSCode 安装最新的 Rust 扩展,即vscode-rust。还会看到 RustyCode 和 vsc-rustfmt 存在,但vscode-rust取代了它们。您可以通过使用命令Ctrl+P并键入来安装它ext install vscode-rust,或者通过在扩展选项卡上搜索它来安装它,其按钮是左侧工具栏上的最后一个。可能需要重新加载才生效。
  • 使用 VSCode 设置服务器。设置File -> Preferences -> Settings。它们的工作方式是使用左窗格覆盖右窗格中显示的任何默认设置。
    这是需要添加的内容:
"rust.rustLangSrcPath": "<path of your rust src>",
"rust.rls": {
    "executable": "rustup",
    "args": [
        "run",
        "nightly",
        "rls"
    ],
    "env": {
        "RUST_LOG": "rls=debug"
    }
},

你的 rust src 应该像C:\Users\<username>\.rust\rust\src.

  • 如果您已经安装,rustfmt您可能还需要添加:
"editor.formatOnSave": true,
"rustfmt.bin": "C:/Users/<username>/.cargo/bin/rustfmt.exe"

或者rustfmt可执行文件的位置。

现在应该能够在 VSCode 中编译、运行、获取自动完成和错误列表等。

可以使用附带的PowerShell的运行cargo,或者可以使用插件的cargo:Ctrl+ Shift+P和类型cargo,以获取列表。


调试设置

  • 首先,添加到设置:
"debug.allowBreakpointsEverywhere": true,
  • 然后搜索ms-vscode.cpptools扩展并安装它。它显示的名称是“C/C++”。您应该将 rustup 工具链设置为msvc.
  • 需要添加一个调试配置,Debug -> Add Configuration...它应该为您提供一个包含“C++ (Windows)”的选择。
  • 选择它,应该会创建一个新launch.json文件,您需要为您的项目自定义该文件,最终应如下所示:
{
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceRoot}/target/debug/<yourprojectname>.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true
        }
    ]
}
  • 现在应该可以设置断点,然后执行Debug -> Start Debugging或点击F5

彩蛋:更改编辑器主题/字体

VSCode 还提供了大量可选主题列表,请执行File -> Preferences -> Color Theme-> Install Additional Color Themes.... 这里使用的是Harmonic16 Light 主题

使用Anonymous Pro作为所有代码编辑的字体。可以设置它并更改大小,方法是在设置中添加这些行:

"editor.fontFamily": "<your font>, Consolas, 'Courier New', monospace",
"editor.fontSize": 16,

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表