有emacs搭建rust开发环境的教程么?

这里有没有人用emacs(spacemacs)开发rust?开发环境都怎么搭建呢?尤其是代码索引,跳转。其次是自动补全。错误提示之类。

多谢。

开箱即用

只需要安装 GitHub - rust-lang/rust-analyzer: A Rust compiler front-end for IDEs 其他都不需要设置

2 个赞

rust的配置,如果结合lsp client的话,基本上就是零配置,即:无需配置。只要对应的language server(rust-analyzerrls)在PATH中即可。可用的lsp client有:

  • lsp-bridge,快!真的快!
  • eglot,轻。
  • lsp-mode,大而全。
  • lspce,依赖少。
3 个赞

lspce其实也挺快的,如果比eglot/lsp-mode慢,那可以当有bug看,我会努力解决。

1 个赞

好像需要安装 rust-mode? 我是按了 rust-mode 才能用的。。。还有类似的 go-mode。

哈哈哈,这是必然。没有mode肯定是不行的。

真是相当的靠谱!!!!

:joy: asdasdasd

我用rustic + lsp-mode,server用rls或者rust-analyzer都行。

1 个赞

cargo, rust-mode, lsp-mode 只用了这三个,感觉体验很不错。

eglot + rust-mode + rust-analyzer

全部 rust 配置

;; Eglot
(require 'eglot)
(add-hook 'rust-mode-hook 'eglot-ensure)

(require 'corfu)
(setq corfu-auto t)
(setq corfu-quit-at-boundary t)
(global-corfu-mode)
1 个赞

我设置完成之后,随便打开一个网上下的rust项目。emacs的message提示出错。貌似是缺环境变量。但是按照rustup的安装说明也没提环境变量之类的。下面好也有lsp bridge相关的出错提示。

Spacemacs is ready. Loading /home/lch/.emacs.d/.cache/company-statistics-cache.el (source)…done [yas] Prepared just-in-time loading of snippets successfully. Loading /home/lch/.emacs.d/.cache/recentf…done Skipping check for new version (reason: dotfile) Package cl is deprecated eldoc error: (user-error No such directory: /usr/local/src/rust/src. Please set ‘racer-rust-src-path’ or ‘RUST_SRC_PATH’) [LSP-Bridge] Start LSP server (rust-analyzer) for /home/lch/download/rust/tikv… [LSP-Bridge] Start LSP server (rust-analyzer) for /home/lch/download/rust/tikv with ’project’ mode, enjoy hacking! deferred error : (error “"TypeError(\"object of type ‘Symbol’ has no len()\")"”) [LSP-Bridge] waiting for cargo metadata or cargo check [2 times] deferred error : (error “"TypeError(\"object of type ‘Symbol’ has no len()\")"”) [LSP-Bridge] waiting for cargo metadata or cargo check deferred error : (error “"TypeError(\"object of type ‘Symbol’ has no len()\")"”)

No such directory: /usr/local/src/rust/src

Please set ‘racer-rust-src-path’ or ‘RUST_SRC_PATH’

应该是 rust 的 std 包没有下?

解决方案

按照 User Manual 说明 已经rustup component add rust-src 下载了源码。但不知道是不是std的包。

这个源码没有装到 /usr/local/src/rust/src 目录。是在.rustup 目录下。

我用的是spacemacs。加了rust layer。貌似它在用racer。racer能禁用么?或者这个可以和lsp-bridge共存?

Spacemacs is ready. Loading /home/lch/.emacs.d/.cache/company-statistics-cache.el (source)…done [yas] Prepared just-in-time loading of snippets successfully. Loading /home/lch/.emacs.d/.cache/recentf…done Skipping check for new version (reason: dotfile) Package cl is deprecated Opening file/dir: ~/download/rust/tikv/src/server/proxy.rs proxy.rs has auto save data; consider M-x recover-this-file [LSP-Bridge] Start LSP server (rust-analyzer) for /home/lch/download/rust/tikv… [LSP-Bridge] Start LSP server (rust-analyzer) for /home/lch/download/rust/tikv with ’project’ mode, enjoy hacking! [LSP-Bridge] waiting for cargo metadata or cargo check [8 times] Error in post-command-hook (winner-save-old-configurations): (wrong-type-argument frame-live-p #<dead frame acm frame 0x55690e585978>) [LSP-Bridge] waiting for cargo metadata or cargo check Company: An error occurred in auto-begin Company: backend company-capf user-error: /home/lch/.cargo/bin/racer exited with 127. ‘M-x racer-debug’ for more info eldoc error: (user-error /home/lch/.cargo/bin/racer exited with 127. ‘M-x racer-debug’ for more info) [LSP-Bridge] waiting for cargo metadata or cargo check [4 times] Mark set Quit

lsp-bridge不需要其他任何补全的前后端,你都禁用掉。

我没有开其他的补全。我只是在spacemacs中加了 rust layer。可能是这里它自己加的。 你的意思是把rust layer去掉? 那rust mode 是不是也会一起去掉了。

只保留rust-mode

我把spacemacs 的rust layer删掉。然后在 dotspacemacs-additional-packages 中加上rust-mode。现在可以补全了。

不过我在代码中随便敲几个字母的时候,messagen 里仍然会出下面这个错误提示,不知道是否正常: deferred error : (error “"TypeError(\"object of type ‘Symbol’ has no len()\")"”)