CLion 2020.1 Help

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

  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 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 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.

    macro expansion engine

  • On-the-fly code analysis with a list of Rust-specific inspections.

    To explore 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:

    implementing members using quick-fix

  • 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.

    inserting a live template

  • 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.png, 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 Ctrl twice to invoke the Run Anything popup:

run cargo commands

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

temporary cargo command configurations

You can edit, save, or remove configurations in Run | Edit Configurations.

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

test runner

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 Cargo Command run/debug configuration

    1. In the Run | Edit Configurations dialog, click 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.png 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.png to profile your application or icons.actions.startMemoryProfile.png 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.

    1. Prepare a debug binary (take a look at these instructions on cross-compilation).

    2. Create a GDB Remote debug configuration and fill in the settings:

      GDB remote debug configuration for a Rust project

      As a symbol file, try using the binary itself. By default, it is placed into the ~/target/debug/ folder.

    3. 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:

      debugging a Rust program remotely

    Last modified: 06 July 2020