preface
The official plug-in experience of rust’s vscode is often unsatisfactory. When I visited the community today, I found RLS 2.0 – trust analyzer,
After the experience, I feel that although there are still many flaws in trust analyzer, it is at least better than RLS 1.0. I hope the rust Working Group
Put in more energy to improve the editor experience.
install
Need nodejs 10 + and NPM, you can refer tonode.js and npmIt will not be repeated.
You may also need to install the trust standard library
rustup component add rust-src
ensurecode
The order is inPATH
Environment variables, and uninstall the rust plug-in to avoid conflicts, execute the following named installation of rust analyzer
git clone https://github.com/rust-analyzer/rust-analyzer.git --depth 1
cd rust-analyzer
cargo install-ra
If there is no error in the installation, this will install the RLS and vscode plug-ins on the machine.
If you want to update the trust analyzer, pull the latest code and run againcargo install-ra
That’s it.
to configure
Open vscode, [[Ctrl +,]] search for trust to see all the configuration of the trust analyzer.
Common configurations are:
{
"rust- analyzer.enableCargoWatchOnStartup ":" true "// auto open cargo watch when opening project
"rust- analyzer.highlightingOn ": true, // override built-in syntax
"rust- analyzer.lruCapacity ": 1000, // analyzer maximum cache depth
}
More configuration referencesettings
characteristic
Workspace symbol search [[Ctrl + T]]
In addition to vscode’s own fuzzy search, trust analyzer also uses#
and*
Enhance fuzzy search function. Use the shortcut key [[Ctrl + T]] to open the symbol search, enter the following content in the search box, and ignore the beginning#
Symbol
Foo
Search the current workspace for theFoo
Types offoo#
Search the current workspace forfoo
Function ofFoo*
At allrely on, includingstdlib
The search name in isFoo
Types offoo#**
Search for all dependencies namedfoo
Function of
Current document symbol search [[Ctrl + Shift + O]]
Provides a complete symbol search capabilities, detailed use, please refer to vscode documentation. PS: one of my favorite features is in@
Add after:
All symbols can be classified, neat and beautiful.
Input assist
Although the document says that trust analyzer has the following features, it doesn’t seem to work in my vscode
Trust analyzer provides assistance in entering specific characters:
- input
let =
If=
Then a known expression will try to add it automatically;
- When you type in a comment, if you change the line, you will automatically add a comment symbol at the beginning of the line
- Input in chain call
.
It indents automatically
Code assist
Trust analyzer adds several useful code tips or AIDS.
Structure code assist
If you move the cursor over the structure name, usectrl + .
You can see it’s coming upadd #[derive]
andadd impl
Two tips, as the name suggestsadd #[derive]
Is to addderive
andadd impl
Will be added automaticallyimpl
Statement block.
Auto add missingtrait
Member method
Add a trait and structure
trait Foo {
fn foo(&self);
fn bar(&self);
fn baz(&self);
}
struct S;
If the structure wants to implement the trait, you can enter theimpl Foo for S {}
To see the auxiliary prompt on the left, press [[Ctrl +.]] to see itadd missing impl members
, enter and confirm
The following code will be automatically filled in
impl Foo for s {
fn foo(&self) { unimplemented!() }
fn bar(&self) { unimplemented!() }
fn baz(&self) { unimplemented!() }
}
I think this function is very convenient, especially when I realize some unfamiliar traits, I don’t need to look through the definition of trait.
Path import
Suppose there’s a piece of code like this
impl std::fmt::Debug for Foo {
}
Move the cursor toDebug
After using code assist will help you refactor code as follows
use std::fmt::Debug;
impl Debug for Foo {
}
Changing function visibility
When the cursor moves to the private function name, you can quickly change the function topub
orpub (crate)
Fill pattern matching branch
Suppose there is an enumerator
enum A {
As,
Bs,
Cs(String),
Ds(String, String),
Es{x: usize, y: usize}
}
fn main() {
let a = A::As;
match a {}
}
In the inputmatch a {}
Post code assist will automatically help you to expand the matching, such as:
fn main() {
let a = A::As;
match <|>a {
A::As => (),
A::Bs => (),
A::Cs(_) => (),
A::Ds(_, _) => (),
A::Es{x, y} => (),
}
}
It is also a very practical function
In addition to the above auxiliary features, trust analyzer has more code auxiliary features, which you can refer to if you are interestedCode Actions(Assists)
Magic fill
This function gives me the feeling of using IPython%command
Magic command, cool and practical.
Suppose there is a function as follows:
foo() -> bool { true }
Follow after calling function.if
Then press [[tab]] to expand toif foo() {}
. All expandable expressions are as follows:
expr.if
->if expr {}
expr.match
->match expr {}
expr.while
->while expr {}
expr.ref
->&expr
expr.refm
->&mut expr
expr.not
->!expr
expr.dbg
->dbg!(expr)
In addition, there are the following snippets in the expression
pd
->println!("{:?}")
ppd
->println!("{:#?}")
In the module, snippets are provided with test methods
tfn
->#[test] fn f(){}
Conclusion:
The new RLS provides more intimate functions than the original RLS, but some basic functions are not as good as the original RLS, which may be due to the lack of snippets,
I’m not used to it at first, but I’m familiar with it as long as I feel good.
This article refers to the document of trust analyzerrust-analyzer-Github
This work adoptsCC agreementReprint must indicate the author and the link of this article