使用VSCode进行Rust开发

用了一段时间CLion,决定换到VSCode了,原因有下:

  1. IntelliJ Rust插件解析proc macro很慢,Expanding macros导致修改某些含有多个macro调用的源文件很慢;

  2. rust-analyzer提供了更好的LSP支持,现阶段IntelliJ Rust仍然使用JetBrains自己写的Engine,估计后期会像clangd一样引入rust-analyzer;

  3. VSCode整体流畅度更高。之前测试过给CLion使用带有Metal支持(Project Lanai)的测试版JDK,仍然没有太大改观。

安装Rust

通过Homebrew

$> brew install rustup
$> rustup install nightly # 也可以使用stable channel

# 配置环境变量
$> echo 'export PATH=$HOME/.cargo/bin:$PATH' >> $HOME/.zshrc

# 测试安装成功
$> rustc --version

rustc 1.54.0-nightly (3e99439f4 2021-05-17)

VSCode扩展

  1. rust-analyzer (扩展页面, 主页):提供Rust LSP支持。rust-analyzer比RLS体验更好,而且未来会作为RLS2.0的一部分替代现在的RLS;

  2. Better TOML (扩展页面):TOML支持,提供高亮及语法验证;

  3. CodeLLDB (扩展页面):Rust调试功能;

  4. Bookmarks (扩展页面):代码书签。快捷键Command + Option + K

  5. GitLens (扩展页面): Git功能增强;

还有一些UI相关的扩展,具体如下:

  1. Auto Hide (扩展页面): 自动隐藏PanelSidebar,无需按下Command + JCommand + B

  2. Customize UI (扩展页面): 合并窗口标题栏及标签页栏。该扩展依赖Monkey Patch扩展,安装Cutomize UI时会自动安装;

配色

根据个人喜好酌情配置…… 我比较懒,一般就用默认主题+改变一下背景色,纯白色看久了有点累。

下面是我自己根据多个主题缝合的配色,来源如下:

  1. VSCode 默认主题 Default Light+
  2. JetBrains Rider默认的C#主题;
  3. rustfmt文档中的示例代码配色。

将如下配置放入VSCode的settings.json中:

"editor.tokenColorCustomizations": {
    "[Default Light+]": {
        "comments": "#8c8c8c",
        // "keywords": "#0033b3",
        // "variables": "#000000",
        "textMateRules": [
            // {
            //     "scope": "entity.name.type.rust",
            //     "settings": {
            //         "foreground": "#FF0000"
            //     }
            // },
            {
                "scope": "meta.function.definition.rust",
                "settings": {
                    "foreground": "#00627a"
                }
            },
            {
                "scope": "meta.attribute.rust",
                "settings": {
                    "foreground": "#9e880d"
                }
            },
            {
                "scope": "variable.language.self.rust",
                "settings": {
                    "foreground": "#ca233a",
                }
            },
            {
                "scope": "entity.name.function.macro.rust",
                "settings": {
                    "foreground": "#dd6718",
                }
            }
        ],
        
    }
},
"editor.semanticTokenColorCustomizations": {
    "[Default Light+]": {
        "enabled": true,
        "rules": {
            // () in attributes
            "parenthesis.attribute:rust": "#9e880d",
            // Variable
            "angle:rust": "#052150",
            "comma:rust": "#052150",
            "brace:rust": "#052150",
            "interface:rust": "#052150",
            "namespace:rust": "#052150",
            "operator:rust": "#052150",
            "parameter:rust": "#052150",
            "parenthesis:rust": "#052150",
            "property:rust": "#871094",
            "punctuation:rust": "#052150",
            // "struct:rust": "#052150",
            "variable:rust": "#052150",
            "variable.static.constant:rust": {
                "foreground": "#871094",
                "italic": true,
            },

            // Method / function
            "method:rust": "#5e2bb4",
            "method.static.trait": {
                "italic": true,
            },
            "function.declaration:rust": "#5e2bb4",
            "function.attribute:rust": "#9e880d",
            // Macro
            "macro:rust": "#dd6718",
            // Keyword
            "keyword:rust": "#ca233a",
            "builtinType:rust": "#ca233a",
            // Enum
            "enum:rust": "#0e72a4",
            "enumMember:rust": {
                "foreground": "#0e72a4",
                "italic": true
            },
            // Comment
            "comment:rust": "#8c8c8c",
            // Type parameter
            "typeParameter:rust": {
                "foreground": "#43a047",
                "italic": true,
                "bold": true
            },
            // Lifetime
            "lifetime:rust": "#20999d",
            // Unresolved reference
            "unresolvedReference": {
                "foreground": "#d50000",
                // "underline": true,
            }
        },
    },
},
"workbench.colorCustomizations": {
    "statusBar.background": "#8d6e63",
    "activityBar.background": "#424242",
    "input.background": "#f5f5f5",
    "menu.background": "#f5f5f5",
    "sideBar.background": "#e0e0e0",
    "panel.background": "#f5f5f5",
    "editor.background": "#f5f5f5",
},

效果图

最终开发环境效果如下:

Full UI

Auto Complete

Debugging

Color Scheme