Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 234
Jay Lee Avatar asked May 20 '20 12:05

Jay Lee


People also ask

How does rust-analyzer work?

On the highest level, rust-analyzer is a thing which accepts input source code from the client and produces a structured semantic model of the code. More specifically, input data consists of a set of test files ( (PathBuf, String) pairs) and information about project structure, captured in the so called CrateGraph .

Should I use rust-analyzer?

We highly recommend giving rust-analyzer a spin today, even if it will take some more time for us to complete the due process and switch from the existing officially recommended LSP implementation (RLS) properly.

Where are rust-analyzer logs?

Log messages are printed to stderr, in VS Code you can see then in the Output > Rust Analyzer Language Server tab of the panel. To see more logs, set the RA_LOG=info environment variable, this can be done either by setting the environment variable manually or by using rust-analyzer.

How do I run a VS Code in rust?

Install the rust-analyzer extension# You can find and install the rust-analyzer extension from within VS Code via the Extensions view (Ctrl+Shift+X) and searching for 'rust-analyzer'. You should install the Release Version.


1 Answers

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

like image 155
fannheyward Avatar answered Sep 20 '22 04:09

fannheyward