aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
Diffstat (limited to 'docs')
-rw-r--r--docs/dev/lsp-extensions.md3
-rw-r--r--docs/dev/style.md105
-rw-r--r--docs/user/manual.adoc15
3 files changed, 102 insertions, 21 deletions
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md
index 43a69d6ce..780f5cb91 100644
--- a/docs/dev/lsp-extensions.md
+++ b/docs/dev/lsp-extensions.md
@@ -392,7 +392,10 @@ rust-analyzer supports only one `kind`, `"cargo"`. The `args` for `"cargo"` look
392{ 392{
393 workspaceRoot?: string; 393 workspaceRoot?: string;
394 cargoArgs: string[]; 394 cargoArgs: string[];
395 cargoExtraArgs: string[];
395 executableArgs: string[]; 396 executableArgs: string[];
397 expectTest?: boolean;
398 overrideCargo?: string;
396} 399}
397``` 400```
398 401
diff --git a/docs/dev/style.md b/docs/dev/style.md
index 59067d234..7a64a0d22 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -186,6 +186,31 @@ impl Person {
186} 186}
187``` 187```
188 188
189## Constructors
190
191Prefer `Default` to zero-argument `new` function
192
193```rust
194// Good
195#[derive(Default)]
196struct Foo {
197 bar: Option<Bar>
198}
199
200// Not as good
201struct Foo {
202 bar: Option<Bar>
203}
204
205impl Foo {
206 fn new() -> Foo {
207 Foo { bar: None }
208 }
209}
210```
211
212Prefer `Default` even it has to be implemented manually.
213
189## Avoid Monomorphization 214## Avoid Monomorphization
190 215
191Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*. 216Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*.
@@ -223,6 +248,8 @@ fn frbonicate(f: impl AsRef<Path>) {
223 248
224# Premature Pessimization 249# Premature Pessimization
225 250
251## Avoid Allocations
252
226Avoid writing code which is slower than it needs to be. 253Avoid writing code which is slower than it needs to be.
227Don't allocate a `Vec` where an iterator would do, don't allocate strings needlessly. 254Don't allocate a `Vec` where an iterator would do, don't allocate strings needlessly.
228 255
@@ -242,6 +269,8 @@ if words.len() != 2 {
242} 269}
243``` 270```
244 271
272## Push Allocations to the Call Site
273
245If allocation is inevitable, let the caller allocate the resource: 274If allocation is inevitable, let the caller allocate the resource:
246 275
247```rust 276```rust
@@ -257,6 +286,9 @@ fn frobnicate(s: &str) {
257} 286}
258``` 287```
259 288
289This is better because it reveals the costs.
290It is also more efficient when the caller already owns the allocation.
291
260## Collection types 292## Collection types
261 293
262Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`. 294Prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`.
@@ -334,27 +366,66 @@ People read things from top to bottom, so place most important things first.
334 366
335Specifically, if all items except one are private, always put the non-private item on top. 367Specifically, if all items except one are private, always put the non-private item on top.
336 368
337Put `struct`s and `enum`s first, functions and impls last.
338
339Do
340
341```rust 369```rust
342// Good 370// Good
343struct Foo { 371pub(crate) fn frobnicate() {
344 bars: Vec<Bar> 372 Helper::act()
345} 373}
346 374
347struct Bar; 375#[derive(Default)]
376struct Helper { stuff: i32 }
377
378impl Helper {
379 fn act(&self) {
380
381 }
382}
383
384// Not as good
385#[derive(Default)]
386struct Helper { stuff: i32 }
387
388pub(crate) fn frobnicate() {
389 Helper::act()
390}
391
392impl Helper {
393 fn act(&self) {
394
395 }
396}
348``` 397```
349 398
350rather than 399If there's a mixture of private and public items, put public items first.
400If function bodies are folded in the editor, the source code should read as documentation for the public API.
401
402Put `struct`s and `enum`s first, functions and impls last. Order types declarations in top-down manner.
351 403
352```rust 404```rust
405// Good
406struct Parent {
407 children: Vec<Child>
408}
409
410struct Child;
411
412impl Parent {
413}
414
415impl Child {
416}
417
353// Not as good 418// Not as good
354struct Bar; 419struct Child;
355 420
356struct Foo { 421impl Child {
357 bars: Vec<Bar> 422}
423
424struct Parent {
425 children: Vec<Child>
426}
427
428impl Parent {
358} 429}
359``` 430```
360 431
@@ -371,6 +442,18 @@ Default names:
371* `n_foo` -- number of foos 442* `n_foo` -- number of foos
372* `foo_idx` -- index of `foo` 443* `foo_idx` -- index of `foo`
373 444
445Many names in rust-analyzer conflict with keywords.
446We use mangled names instead of `r#ident` syntax:
447
448```
449struct -> strukt
450crate -> krate
451impl -> imp
452trait -> trait_
453fn -> func
454enum -> enum_
455mod -> module
456```
374 457
375## Early Returns 458## Early Returns
376 459
diff --git a/docs/user/manual.adoc b/docs/user/manual.adoc
index 46e7bd091..7e8adcdb7 100644
--- a/docs/user/manual.adoc
+++ b/docs/user/manual.adoc
@@ -116,6 +116,7 @@ Here are some useful self-diagnostic commands:
116* **Rust Analyzer: Show RA Version** shows the version of `rust-analyzer` binary. 116* **Rust Analyzer: Show RA Version** shows the version of `rust-analyzer` binary.
117* **Rust Analyzer: Status** prints some statistics about the server, and dependency information for the current file. 117* **Rust Analyzer: Status** prints some statistics about the server, and dependency information for the current file.
118* To enable server-side logging, run with `env RA_LOG=info` and see `Output > Rust Analyzer Language Server` in VS Code's panel. 118* To enable server-side logging, run with `env RA_LOG=info` and see `Output > Rust Analyzer Language Server` in VS Code's panel.
119* To log project loading (sysroot & `cargo metadata`), set `RA_LOG=project_model=debug`.
119* To log all LSP requests, add `"rust-analyzer.trace.server": "verbose"` to the settings and look for `Rust Analyzer Language Server Trace` in the panel. 120* To log all LSP requests, add `"rust-analyzer.trace.server": "verbose"` to the settings and look for `Rust Analyzer Language Server Trace` in the panel.
120* To enable client-side logging, add `"rust-analyzer.trace.extension": true` to the settings and open `Output > Rust Analyzer Client` in the panel. 121* To enable client-side logging, add `"rust-analyzer.trace.extension": true` to the settings and open `Output > Rust Analyzer Client` in the panel.
121 122
@@ -260,16 +261,7 @@ If you get an error saying `No such file or directory: 'rust-analyzer'`, see the
260 261
261=== GNOME Builder 262=== GNOME Builder
262 263
263Prerequisites: You have installed the <<rust-analyzer-language-server-binary,`rust-analyzer` binary>>. 264GNOME Builder 3.37.1 and newer has native `rust-analyzer` support. If the LSP binary is not available, GNOME Builder can install it when opening a Rust file.
264
265Gnome Builder currently has support for RLS, and there's no way to configure the language server executable. A future version might support `rust-analyzer` out of the box.
266
2671. Rename, symlink or copy the `rust-analyzer` binary to `rls` and place it somewhere Builder can find (in `PATH`, or under `~/.cargo/bin`).
2682. Enable the Rust Builder plugin.
269
270==== GNOME Builder (Nightly)
271
272https://nightly.gnome.org/repo/appstream/org.gnome.Builder.flatpakref[GNOME Builder (Nightly)] has now native support for `rust-analyzer` out of the box. If the `rust-analyzer` binary is not available, GNOME Builder can install it when opening a Rust source file.
273 265
274== Non-Cargo Based Projects 266== Non-Cargo Based Projects
275 267
@@ -295,6 +287,9 @@ interface JsonProject {
295} 287}
296 288
297interface Crate { 289interface Crate {
290 /// Optional crate name used for display purposes, without affecting semantics.
291 /// See the `deps` key for semantically-significant crate names.
292 display_name?: string;
298 /// Path to the root module of the crate. 293 /// Path to the root module of the crate.
299 root_module: string; 294 root_module: string;
300 /// Edition of the crate. 295 /// Edition of the crate.