blog
blog copied to clipboard
Rust: with Clion/IntelliJ IDEA Community/Vscode
related:
- ✅ https://github.com/better-rs/.github/discussions/6
- ✅ https://github.com/better-rs/.github/discussions/2
- ✅ https://github.com/better-rs/.github/discussions/4
- ✅ #376
- ✅ #247
- ✅ #375
- ✅ #343
Rust 开发环境搭建:
- ✅ 首选推荐使用 Clion/IntelliJ IDEA Community 社区版, 比
vscode + ra
代码索引+跳转+补全能力更强. - ✅
vscode+ra
目前无法跳转宏动态导模块
的代码,Clion
可以.
基本步骤:
- ✅ 安装 rust
- ✅ 安装 rust-analyzer.
- ✅ 安装 Clion/IntelliJ IDEA Community 社区版/Vscode.
- ✅ 安装插件:
- vscode: rust-analyzer 插件
-
clion/ IntelliJ IDEA Community 社区版
: intellij-rust 插件 | intellij-rust 文档
- ✅ 配置 Clion/
IntelliJ IDEA Community 社区版
/Vscode.
PS:
安装 IntelliJ IDEA Community 社区版 + intellij-rust 插件,
然后 action 开启 `org.rust.cargo.evaluate.build.scripts` 选项。
也是支持宏代码跳转的。亲测有效。依然比 vscode + ra 强大。
使用 rustup 安装 rust:
- https://www.rust-lang.org/tools/install
- https://rustup.rs/
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
查看安装 rust 版本:
➤ rustup show
Default host: aarch64-apple-darwin
rustup home: /Users/dev/.rustup
installed toolchains
--------------------
stable-aarch64-apple-darwin
nightly-2022-07-31-aarch64-apple-darwin
nightly-aarch64-apple-darwin (default)
1.61.0-aarch64-apple-darwin
active toolchain
----------------
nightly-aarch64-apple-darwin (default)
rustc 1.63.0-nightly (12cd71f4d 2022-06-01)
➤ cargo version
cargo 1.63.0-nightly (38472bc19 2022-05-31)
验证 IDE 宏补全 - 示例项目:
- ✅ https://github.com/hyperium/tonic
- https://github.com/hyperium/tonic#getting-started

Rust LSP - rust-analyzer: for Vscode
- ✅ https://rust-analyzer.github.io/manual.html#configuration
- ✅ 详细配置参数表.
pb 生成代码补全:
- ✅ https://github.com/hyperium/tonic/issues/512
- ✅ https://github.com/rust-lang/rust-analyzer/issues/3767
- 确认当前并不支持.
intellij-rust 代码补全 - 核心设置: for Clion
- ✅ https://github.com/intellij-rust/intellij-rust/issues/6571
- ✅ https://github.com/intellij-rust/intellij-rust/issues/4631
org.rust.cargo.evaluate.build.scripts = true
Rust 配置 Clion 开发环境:
- ✅ 安装 Clion/IntelliJ IDEA Community 社区版
- ✅ 安装插件: intellij-rust 插件 | intellij-rust 文档
基本设置:
- ✅ 配置 cargo 等:

项目主动激活索引:
- ✅ 新项目, 主动激活
索引
的方法: - ✅ 项目中如果有多个 workspace, 需要手动指定
Cargo.toml
文件, 来激活IDE 创建索引.

激活宏生成的代码补全:
- ✅ https://plugins.jetbrains.com/plugin/8182-rust/docs/rust-faq.html#experimental-features
- ✅ https://github.com/intellij-rust/intellij-rust/issues/6571
- ✅ https://www.reddit.com/r/rust/comments/ry0phj/notes_on_rust_ides_clion_vs_rust_analyzer/
org.rust.cargo.evaluate.build.scripts
说明:
- ✅ JB IDE 快捷键
双 shift
, 然后输入:experimental
, 切换到 Action, 选择打开 "实验特性". - ✅ 激活后, IDE 宏跳转代码, 可能并不会立即生效.
- 需要手动清除 IDE 当前项目的
索引 cache
. - 关闭IDE, 重新打开项目. (后面有详细截图)
- 需要手动清除 IDE 当前项目的
具体步骤:
- ✅ 找到
action
Tab: 快捷键双 shift
, 然后输入:experimental
, 切换到 Action,

- ✅ 激活 action 实验选项:

- ✅ 如果IDE 没有立即生效, 手动清除cache, 重建项目索引, 重新打开工程:

配置成功的效果:
- ✅ 感受 clion 的强大:
- ✅ 对比 vscode 的支持能力差距:
Rust 配置 Vscode:
- https://code.visualstudio.com/docs/languages/rust
- https://juejin.cn/post/7038877104175824909
插件配置:
rust-analyzer:
生成代码补全支持:
- https://users.rust-lang.org/t/managing-generated-code-with-rust-analyzer/64863
clion 的宏补全能力:
-
示例代码: https://github.com/better-rs/learn-rs/blob/main/crates/rs-tonic/readme.md
-
示例图:
Clion index 炸机修复:
# clion功能不行的时候,用这个命令:
cargo metadata --verbose --format-version 1 --all-features
1
1