Emacs Rust Mode
Table of Contents
2021-08-25 夜,晴
在Emacs下,Rust开发有两个选择:
该如何选择呢?
1. Rust Mode选择
我选Rust Mode。这里说一下我的理由:
- Rust Mode在README里推荐了Rustic,说明Rust Mode作者较为宽厚,
- Rustic对自己分叉Rust Mode的行为进行了辩解,但没有多少站得住的理由,
- Rust Mode只做最核心的内容,周边留给其他包做,这体现Unix编程哲学,
- Rustic啥都做但有些做的不好,比如Cargo命令。
我用Emacs自带的Package管理:
package-install rust-mode package-install cargo
我习惯把code format绑到 M-q 上:
(define-key rust-mode-map (kbd "M-q") 'rust-format-buffer)
Cargo.el 所提供的命令 cargo-process-add 需要 cargo-edit 支持:
cargo install cargo-edit
至此,语法加亮,代码规整,常见命令支持都有了。还差一个代码补齐和跳转, 由Emacs的LSP能力补足。
2. LSP的选择
Emacs下LSP(Language Server Protocol)有两个选择: 1. LSP, 2. Eglot
这又如何选呢?
LSP我在沉迷Go语言的时候用过,怎么说呢? LSP不坏,但远算不上优秀。 为提供酷炫功能,LSP用了一些不标准的Lisp API,以至于Emacs升级到28.0之 后,这些API被废弃掉了,LSP就常有抽风行为。我不可能为了一个勉强工作的 LSP把Emacs版本固守在27.0。于是我转向了Eglot:
package-delete lsp-mode package-install eglot
他们显然都不是平心静气,但如果你是一个好的程序员,你能很容易分辨出是非 高下来。于是我毫不犹豫地继续使用Eglot。
Eglot需要装上=rls=才能工作:
rustup update rustup component add rls rust-analysis rust-src
给=rust-mode=加上=eglot=支持:
(add-hook 'rust-mode-hook 'eglot-ensure)
重启Emacs,搞定。最后写几句给程序员的话。