CLion 2019.2 Help

Rust

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

  1. Install the Rust package using rustup.rs or other installation options.

  2. Install the IntelliJ Rust plugin either directly from the plugin repository or right from CLion: go to Settings / Preferences | Plugins, 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.

  1. Go to File | Open and select the project root directory containing Cargo.toml.

  2. Check the Cargo tool window (View | Tool windows | Cargo) to make sure the project was imported successfully.

Create a new Rust project

  1. Start a new project (File | New Project) and choose Rust as your project type. In the New Project dialog, provide the paths to the toolchain and the standard library (which you can download via rustup right from the wizard):

    creating a new Rust project

  2. Click Create, and CLion will generate a new project with the following structure:

    new Rust project structure

Configure the project settings

Go to Settings/Preferences | Languages & Frameworks and navigate to the Rust node of the settings tree:

  • In 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:

    project settings
  • In the Cargo dialog, you can configure the external linter if you plan to use one. Choose between Cargo Check and Clippy:

    cargo settings

Code insight features

IntelliJ Rust plugin is being actively developed, and new features come with every version. In addition to the general code insights like syntax highlighting, code formatting, navigation, and refactorings, the plugin's language support 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 Settings/Preferences | Languages & Frameworks | Rust.

    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 view or edit the inspections, go to Settings/Preferences | Editor | Inspections | Rust:

    Rust inspections

    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:

    code generation for Rust code

    Also, the plugin provides a set of live templates for Rust. You can customize the built-in templates and create your own in Settings/Preferences | Editor | Live Templates | Rust.

  • Quick Documentation popup Ctrl+Q, including documentation for library functions:

    Quick documentation for Rust code

Using Cargo

The Cargo tool window (View | Tool Windows | Cargo) shows all workspace members and targets. From this tool window, you can quickly run a target by clicking on it, refresh your project icons actions refresh svg, reformat it with rustfmt, and run the external linter configured in Settings/Preferences | Languages & Frameworks | Rust | Cargo.

To run a Cargo command, click Run Cargocommand or press Double Ctrl to invoke the Run Anything popup:

When you run a Cargo command, CLion creates a temporary run/debug configuration of the Cargo command type:

temporary cargo command configurations
Go to the Edit Configurations dialog to edit, save, or remove configurations.

Debugging

If required, you can configure the debugger data views in Settings/Preferences | Build, Execution, Deployment | Debugger | Data Views | Rust:

debugger data views options

To start a debug session, select an existing Cargo command configuration, or create a new one.

Create a run/debug configuration

  1. In the Run | Edit Configurations dialog, press plus and choose Cargo Command.

  2. Specify the settings for your Cargo Command configuration:

    Cargo run/debug 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 Debugicons actions startDebugger svg it with all the CLion debug features (breakpoints, variable monitor, stepping, and others) available for your Rust application:

debugging Rust

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 icons toolwindows toolWindowProfiler svg to profile your application or icons actions startMemoryProfile svg to run it with Valgrind memcheck.

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 icons runConfigurations testState run svg the tests or toggle automatic rerun on changes in your code, sort the tests alphabetically icons objectBrowser sorted svg or by duration icons runConfigurations sortbyDuration svg, and open previous results artwork studio icons profiler toolbar clock:

test runner

Last modified: 22 August 2019