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