All of the references for string completion for Emacs rust support seem to require having the nightly rust installed locally. Is there something intrinsic that requires that?
If so, what are the drawbacks of installing nightly rust?
Thanks,
Joel
I’m not familiar with Emacs support for Rust, but generally, the only real disadvantage of using nightly is that it has fresh changes that haven’t been tested very much. Stable releases of Rust have a six-week beta testing period (each beta is branched off at the same time as the previous one becomes stable) and include fixes for regressions discovered during that period before release.
Despite that lack of thorough testing, though, it’s quite rare for nightly to not be usable.
Thanks. Then even if I have to use the nightly, I will probably switch to it. The lack of completion is the one frustration I have with using Emacs for development.
Yours,
Joel
I'm not very familiar with Emacs, since my usage was limited to Doom and Spacemacs a year or so ago. Either way, I don't remember any caveats around needing to use nightly for completions... there were plenty of times I never even had nightly installed. I'm not sure what they used, but that might be worth looking into?
Isn't completion a feature of the LSP-mode they added in emacs30? That should just require rust-analyzer as the LSP AFAIK, not specifically nightly.
Do you know where Emacs is taking list of all std function names? I want to integrate it with my editor too.
Thanks. I had missed that reference.
Joel
Rust has very good support in Emacs.
In stock Emacs since version 29, there is a LSP server called Eglot, which automatically detects rust-analyzer when in PATH and completion does work out of the box.
You just have to install rust-mode package and you are set. And possibly company-mode. Works with stable rust, no nightly required unless project is set to use nightly.
That was what I hoped. Do I have to do anything to turn on Eglot? And do I need to use any specific key strokes to get completion menus? Thank you very much. Joel
Rust-mode is started automatically when rust filetype is opened. You just invoke eglot-ensure to start LSP server and company-mode for comfortable completion support afterwards.
:hook
(rust-mode .
(lambda ()
(company-mode)
(eglot-ensure)))
Or just open rust file in a cargo project and invoke company-mode and eglot manually to test it out.
Completion window pops-up automatically after at least 3 characters are typed in(default threshold) or when invoking company-complete function. In my config I use Ctrl-Tab
:bind (:map company-mode-map
("C-<tab>" . company-complete))
Btw. rust-mode and eglot do much more, like code navigation, refactoring, compilation, running tests or execute package, on-the-fly documentation, type signature hints, placeholders for code completions, on-the-fly code linting for warnings & errors etc.
I tried M-x package-install company and M-x package-isntall eglot, and now apropos does find a bunch of things for both. I am now reading the eglot manual. I am probably not using some fo the rust-mode features I would appreciate, as the only documentation page I found is: GitHub - rust-lang/rust-mode: Emacs configuration for Rust
Thank you,
Joel
Apparently, I am still missing a step. When I invoke M-x eglot on my main.rs buffer, I get told -1: Server died
Yours,
Joel
Which Emacs version do you have? Eglot is built in since Emacs 29, so need not to install.
Without content of actual error message it is hard to guess. Either from *Messages* and/or *EGLOT* buffers.
Can Emacs find rust-analyzer in PATH? I.e. can you run rust-analyzer manually inside cargo project root?
Seems I need to upgrade my Emacs (at 28.1) and figure out how to install rust analyzer. Those are now on the list of things to do.
Thanks,
Joel
My Emacs config for Rust:
(use-package eglot
:hook ((rust-ts-mode rust-mode nix-ts-mode) . eglot-ensure)
:config (add-to-list 'eglot-server-programs '(
(rust-ts-mode rust-mode) . ("rust-analyzer" :initializationOptions (:check (:command "clippy"))))))
(use-package flymake
:hook (prog-mode . flymake-mode))
(use-package rust-mode
:init
(setq rust-mode-treesitter-derive t))
Hope this helps.
I would love to hear feedback from other Emacs users, and compare configurations.
Thanks. That looks like it will be helpful. I installed reust-analyzer and upgraded to emacs 30.1, and have eglot working. Still learning what it does and how to interact with it.
What does treesitter do?
Is there somewhere a tutorial on how to interact with eglot / flymaker? I strongly suspect I am missing much of what it can do for me. (Yes, I looked at the eglot info pages. Because that is compiler and key binding agnostic, I don't really know what it is telling me.)
Yours,
Joel
Thanks,
Joel