rust.md

  1# Rust
  2
  3- Tree Sitter: [tree-sitter-rust](https://github.com/tree-sitter/tree-sitter-rust)
  4- Language Server: [rust-analyzer](https://github.com/rust-lang/rust-analyzer)
  5
  6## Inlay Hints
  7
  8The following configuration can be used to enable inlay hints for rust:
  9
 10```json
 11"inlayHints": {
 12  "maxLength": null,
 13  "lifetimeElisionHints": {
 14  "useParameterNames": true,
 15    "enable": "skip_trivial"
 16  },
 17  "closureReturnTypeHints": {
 18    "enable": "always"
 19  }
 20}
 21```
 22
 23to make the language server send back inlay hints when Zed has them enabled in the settings.
 24
 25Use
 26
 27```json
 28"lsp": {
 29  "$LANGUAGE_SERVER_NAME": {
 30    "initialization_options": {
 31      ....
 32    }
 33  }
 34}
 35```
 36
 37to override these settings.
 38
 39See https://rust-analyzer.github.io/manual.html#inlay-hints for more information.
 40
 41## Target directory
 42
 43The `rust-analyzer` target directory can be set in `initialization_options`:
 44
 45```json
 46{
 47  "lsp": {
 48    "rust-analyzer": {
 49      "initialization_options": {
 50        "rust": {
 51          "analyzerTargetDir": true
 52        }
 53      }
 54    }
 55  }
 56}
 57```
 58
 59A `true` setting will set the target directory to `target/rust-analyzer`. You can set a custom directory with a string like `"target/analyzer"` instead of `true`.
 60
 61## More server configuration
 62
 63Rust-analyzer [manual](https://rust-analyzer.github.io/manual.html) describes various features and configuration options for rust-analyzer language server.
 64Rust-analyzer in Zed runs with the default parameters.
 65
 66### Large projects and performance
 67
 68One of the main caveats that might cause extensive resource usage on large projects, is the combination of the following features:
 69
 70```
 71rust-analyzer.checkOnSave (default: true)
 72    Run the check command for diagnostics on save.
 73```
 74
 75```
 76rust-analyzer.check.workspace (default: true)
 77    Whether --workspace should be passed to cargo check. If false, -p <package> will be passed instead.
 78```
 79
 80```
 81rust-analyzer.cargo.allTargets (default: true)
 82    Pass --all-targets to cargo invocation
 83```
 84
 85Which would mean that every time Zed [auto]saves, a `cargo check --workspace --all-targets` command is run, checking the entire project (workspace), lib, doc, test, bin, bench and [other targets](https://doc.rust-lang.org/cargo/reference/cargo-targets.html).
 86
 87While that works fine on small projects, it does not scale well.
 88
 89The alternatives would be to use [tasks](../tasks.md), as Zed already provides a `cargo check --workspace --all-targets` task and the ability to cmd/ctrl-click on the terminal output to navigate to the error, and limit or turn off the check on save feature entirely.
 90
 91Check on save feature is responsible for returning part of the diagnostics based on cargo check output, so turning it off will limit rust-analyzer with its own [diagnostics](https://rust-analyzer.github.io/manual.html#diagnostics).
 92
 93Consider more `rust-analyzer.cargo.` and `rust-analyzer.check.` and `rust-analyzer.diagnostics.` settings from the manual for more fine-grained configuration.
 94Here's a snippet for Zed settings.json (the language server will restart automatically after the `lsp.rust-analyzer` section is edited and saved):
 95
 96```json5
 97"lsp": {
 98    "rust-analyzer": {
 99        "initialization_options": {
100            // get more cargo-less diagnostics from rust-analyzer,
101            // which might include false-positives (those can be turned off by their names)
102            "diagnostics": {
103                "experimental": {
104                    "enable": true
105                }
106            },
107            // To disable the checking entirely
108            // (ignores all cargo and check settings below)
109            "checkOnSave": false,
110            // To check the `lib` target only.
111            "cargo": {
112                "allTargets": false
113            },
114            // Use `-p` instead of `--workspace` for cargo check
115            "check": {
116              "workspace": false
117            }
118        }
119    }
120}
121```
122
123### Snippets
124
125There's a way get custom completion items from rust-analyzer, that will transform the code according to the snippet body:
126
127```json
128"lsp": {
129    "rust-analyzer": {
130        "initialization_options": {
131            "completion": {
132                "snippets": {
133                    "custom": {
134                        "Arc::new": {
135                            "postfix": "arc",
136                            "body": ["Arc::new(${receiver})"],
137                            "requires": "std::sync::Arc",
138                            "scope": "expr"
139                        },
140                        "Some": {
141                            "postfix": "some",
142                            "body": ["Some(${receiver})"],
143                            "scope": "expr"
144                        },
145                        "Ok": {
146                            "postfix": "ok",
147                            "body": ["Ok(${receiver})"],
148                            "scope": "expr"
149                        },
150                        "Rc::new": {
151                            "postfix": "rc",
152                            "body": ["Rc::new(${receiver})"],
153                            "requires": "std::rc::Rc",
154                            "scope": "expr"
155                        },
156                        "Box::pin": {
157                            "postfix": "boxpin",
158                            "body": ["Box::pin(${receiver})"],
159                            "requires": "std::boxed::Box",
160                            "scope": "expr"
161                        },
162                        "vec!": {
163                            "postfix": "vec",
164                            "body": ["vec![${receiver}]"],
165                            "description": "vec![]",
166                            "scope": "expr"
167                        }
168                    }
169                }
170            }
171        }
172    }
173}
174```