8

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.

Jay Lee
  • 1,684
  • 1
  • 15
  • 27
  • 4
    If you aren't using Cargo, how are you building and running your code? How are you building and linking dependencies like `rand` and `num`, which you will no doubt need for Project Euler? It looks like you are making your life unnecessarily difficult, for no clear benefit. Running `cargo new` for each problem doesn't seem to be _that_ much overhead. Alternatively, you can also have many main programs in `src/bin`. – Sven Marnach May 20 '20 at 15:05
  • I organize my rust leetcode solutions in one single project in different modules with tests. That seems to be solving it for me. – Kuznero May 20 '20 at 20:44
  • @SvenMarnach I didn't know Rust didn't have a built-in solution for rands. Thank you. – Jay Lee May 21 '20 at 01:35
  • @Kuznero I'll take a look at this workaround, thank you – Jay Lee May 21 '20 at 01:35
  • 2
    I still recommend using a single package with multiple main programs in `src/bin`. You can compile them separately with `cargo build --bin ` for the file `src/bin/.rs`, and you can add common library code in `src/lib.rs`, which you can use in any of the binaries. – Sven Marnach May 21 '20 at 20:08
  • If you really want to use single-file Rust code, there is rust-script: https://rust-script.org/. But again, go with @SvenMarnach and make it one project with multiple bin targets. – Unapiedra Dec 15 '20 at 23:46

1 Answers1

2

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

fannheyward
  • 18,599
  • 12
  • 71
  • 109