Tips and tricks for a nice Rust development experience
Using VSCode
- Ensure you're using the
rust-analyzerextension for VSCode, rather than earlier forms of Rust support. - Run
gnwith the--export-rust-projectflag, such as:gn gen out/Release --export-rust-project. ln -s out/Release/rust-project.json rust-project.json- When you run VSCode, or any other IDE that uses
rust-analyzer it should detect the
rust-project.jsonand use this to give you rich browsing, autocompletion, type annotations etc. for all the Rust within the Chromium codebase. - Point rust-analyzer to the rust toolchain in Chromium. Otherwise you will
need to install Rustc in your system, and Chromium uses the nightly
compiler, so you would need that to match. Add the following to
.vscode/settings.jsonin the Chromium checkout (e.g.chromium/src/.vscode/settings.json; create the subdirectory and file if they do not already exist):
This assumes you are working with an output directory like{ // The rest of the settings... "rust-analyzer.cargo.extraEnv": { "PATH": "../../third_party/rust-toolchain/bin:$PATH", } }out/Debugwhich has two levels; adjust the number of..in the path according to your own setup.
Suppressing unused code warnings
We don't want to commit Rust code with unused variables, but work-in-progress changes often temporarily have unused code. The following options exist for dealing with unused code warnings:
- Avoid warnings by prefixing
unused variables, parameters, and other APIs
with an underscore character
(e.g. using
_foorather thanfooas the name). - Temporarily insert
#![allow(unused)] /* DO NOT SUBMIT */at the top of the affected.rsfiles.- See
rustc -W helpfor a list of warnings covered by theunusedgroup of warnings. - You can also use
#![warn(...)]to still see the warnings, but avoid treating them as errors.
- See
- Locally set
treat_warnings_as_errors = falsein yourargs.gn. This will affect all Rust and C/C++ warnings.