Rust Support
Rust is supported in CLion via the IntelliJ Rust plugin, compatible with all IntelliJ-based IDEs. In addition to the language engine and Cargo support, the CLion's implementation of the plugin also provides the debugger for your Rust applications.
Prepare the environment
Install the Rust package using rustup.rs or other installation options.
Install the IntelliJ Rust plugin either directly from the plugin repository or right from CLion: go to , switch to Marketplace and type Rust in the search field, then click Install in the plugin description dialog.
Open a Rust project
Note that only the Cargo-based Rust projects are supported.
Go to Cargo.toml.
and select the project root directory containingCheck the Cargo tool window ( ) to make sure the project was imported successfully.
Create a new Rust project
Start a new project (Rust as your project type.
) and chooseIn the New Project dialog, provide the paths to the toolchain and the standard library (which you can download via rustup right from the wizard):
Click Create, and CLion will generate a new project with the following structure:
Configure the project settings
Go to Rust node of the settings tree:
and navigate to theIn the Rust dialog, adjust the general project settings.
Notice the Expand declarative macros option, with which you can switch to the experimental macro expansion engine:
In the Cargo dialog, you can configure the external linter if you plan to use one. Choose between Cargo Check and Clippy:
Code insight features
IntelliJ Rust is being actively developed, and new features come with every version. In addition to the general code insight features like syntax highlighting, code formatting, navigation, and refactorings, the plugin's language support also includes:
Macro expansion engine for declarative macros.
Currently, there are two macro expansion engines: default and experimental. To switch between them, use the Expand declarative macros option in .
Both of the engines provide code insight features for generated top-level items such as structs, enums, functions, and traits. On top of that, the experimental engine performs highlighting, name resolution, and completion for generated modules and methods from
impl
blocks, and enables navigation in macro calls.On-the-fly code analysis with a list of Rust-specific inspections.
To explore or edit the inspections, go to
:For most of the inspections, quick-fixes are available via Alt+Enter. For example, you can implement missing members via a quick-fix, and the plugin will correctly substitute a generic type parameter and lifetimes:
Various code generation options, for example:
Also, the plugin provides a set of live templates for Rust. You can customize the built-in templates and create your own in .
Quick Documentation popup Ctrl+Q, including documentation for library functions:
Using Cargo
The Cargo tool window ( ) shows all workspace members and targets. From this tool window, you can quickly run a target by clicking on it, refresh your project , reformat it with rustfmt, and run the external linter configured in .
To run a Cargo command, click or press Ctrl twice to invoke the Run Anything popup:
When you run a Cargo command, CLion creates a temporary run/debug configuration of the Cargo command type:
You can edit, save, or remove configurations in
.Testing
When you run the cargo test
command, the plugin employs CLion's built-in test runner to help you explore the results.
The test runner window shows a progress bar for the running tests and groups all tests into a tree view according to their status and duration. From this window, you can rerun the tests or toggle automatic rerun on changes in your code, sort the tests alphabetically or by duration , and open previous results :
Debugging
If required, you can configure the debugger data views in
:To start a debug session, select an existing Cargo command configuration, or create a new one.
Create a Cargo Command run/debug configuration
In the Cargo Command.
dialog, click and chooseSpecify the settings for your Cargo Command configuration:
The Emulate terminal in output console option can be useful for crates like color-backtrace, which behave differently when a process is run under TTY (see the feature description). This option is not available on Windows.
After the configuration is set up, you can Debug it with all the CLion debug features (breakpoints, variable monitor, stepping, and others) available for your Rust application:
For your debug session, you can enable hex view, invoke memory view, and step into disassembly.
CLion's dynamic analysis tools are also available for your Cargo configuration: click to profile your application or to run it with Valgrind memcheck.
Remote debug with GDB/gdbserver
You can debug a Rust program running remotely from CLion on your local machine with the help of the build system-independent GDB Remote debug configuration.
Prepare a debug binary (take a look at these instructions on cross-compilation).
Create a GDB Remote debug configuration and fill in the settings:
As a symbol file, try using the binary itself. By default, it is placed into the ~/target/debug/ folder.
Launch your program remotely under gdbserver, place breakpoints in the code, and start a debug session. You will be able to use all the CLion debug features as if the program was running locally: