Search code examples
vimrustrust-cargolanguage-server-protocolcoc.nvim

Working with single file rust using rust-analyzer


I'm trying to make rust-analyzer (with Neovim) work with a single Rust file. I know that using Cargo should be the default, but I'm trying to solve problems like the ones from project euler, where making a project for each problem seems like an overkill. Furthermore, I solve problems in various languages, so I want to make each code self-contained.

However, with coc-rust-analyzer, it says:

[coc.nvim] rust-analyzer failed to discover workspace, no Cargo.toml found, dirs searched: /Users/jay/some-dir

I just want to run rust-analyzer with this file only. What should I do?

—————

Update: I'm just starting to use Rust, and I used Python, OCaml, C++ for previous problems. I used rustc for a simple problem.

Languages like OCaml provides a (verbose) solution using ocamlfind & ocamlopt, and to use merlin which is a tool for vim and emacs, I only need a top level .merlin file like

PKG core stdio ppx_deriving.std ppx_variants_conv

that lists all the packages I need to use. I could have used dune, which is kind of like the build system part of cargo.

I found that rustc can link external crates, e.g. rustc executable.rs --extern rary=library.rlib && ./executable. I admit this may be more complex than a Cargo based solution, but still makes me wonder if rust-analyzer can only work with Cargo based projects.


Solution

  • rust-analyzer can work for standalone/single rust file, https://github.com/rust-analyzer/rust-analyzer/pull/8955, coc-rust-analyzer supports this too.