When we develop a great rust project, cargo is the best tool. But sometimes the code we want to write is only in one file, such as OJ, or teaching code and etc. At this time, using cargo maybe a bit troublesome.
However, rust-analyzer
doesn't work when meeting single rust file in general.
Using our extension, you can use it normally.
Notice: this extension may not work properly due to
rust-analyzer
, but we will fix this problem as soon as possible.
Make sure to install rust-analyzer
extension first, otherwise our extension won't work.
And then, you should set sysroot
in extension settings.
If you don't know what it is,
you can use rustc --print sysroot
to get it.
- Open a empty folder
- Utilize
rust-project: Initialize
command to activate our extension - Create a
*.rs
file - You can write rust code using rust-analyzer now
If there are already rust code in your folder, this extension will be automatically enabled.
In the above circumstance, all configuration information about your code will be stored in .vscode/settings.json
.
As the quantity of files in your project increases, the size of this file will also grow.
To Address this,
our extension offers a method to manage files within a folder more effectively.
- Create a empty folder in your workspace
- Right click the newly created folder in the explorer
- Select the Option
Create rust-project
- Create a
*.rs
file in the same folder - You can write rust code using rust-analyzer now
Please note that it's currently not possible to create rust-project in a non-empty folder. Doing so would introduce complications for the logic of our extension.
Our extension determines the location of
*.rs
file using longest-path-matching approach. This means that it searches for therust-project.json
file with the longest identical path. otherwise we usesettings.json
file. If creating arust-project.json
in a non-empty folder is allowed, our logic will be broken.While we may consider further improvement in the future, this is the current behavior.
Additionally, you have the option to destroy the rust-project.json
file that you have created.
but ensure that it doesn't contain any *.rs
files.
Please use the command we provided to alter configuration files
such as settings.json
and rust-project.json
instead of making manual changes.
If you're interested in delving deeper into the principles behind this extension, you can refer to the User Manual provided by the rust-analyzer.
Our extension will automatically configure the path of sysroot.
If rust-analyzer still can't find this, you could try restart your VSCode
or press ctrl + ,
to set the path in settings manually.
rust-analyzer need source code of the standard library. If these code doesn't exist in your system, please execute the below command to install manually.
$ rustup component add rust-src
For convenient of reading the settings.json
, we store it in the memory.
This implies that every action within our extension initially modifies the data in memory
and subsequently writes it to the filesystem.
While we don't recommend manual changes to this file, if you find it necessary to make alterations, please use the "Reload Settings File" command to ensure that your changes take effect.