CLion 2022.3 Help

Rust

Rust is supported in CLion via the IntelliJ Rust plugin, compatible with all IntelliJ-based IDEs. The plugin provides Rust language engine with a set of code insight features, Cargo support, and debugger functionality (in CLion and IntelliJ IDEA Ultimate).

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

    Cargo tool window

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 | Languages & Frameworks and navigate to the Rust node of the settings tree.

  • In the Rust dialog, adjust the general project settings.

    General Rust project settings

    Use the Expand declarative macros switcher if you want to disable the default new macro expansion engine (in case it fails for your code at some point). You can switch to the old engine or turn macro expansion off completely.

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

    Cargo settings

    You can enable the external linter to work on the fly, but this might affect performance especially on big projects. To run the linter on demand, use the Run External Linter action.

    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.

      The default engine provides code insight for generated items such as structs, enums, functions, and traits. On top of that, it 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.

      You can explore the inspections and edit their severities and scopes in Settings | 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 Rust-specific code generation options and intention actions, for example:

      code generation for Rust code

      Full list of intentions is available in Settings | Editor | Intentions | Rust.

    • A set of live templates for Rust. You can customize the built-in templates and create your own in Settings | 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 App actions refresh, reformat it with rustfmt, and run the external linter configured in Settings | Languages & Frameworks | Rust | Cargo.

    To run a Cargo command, click Run Cargo Command 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.

    Create a Cargo Command configuration

    1. Go to Run | Edit Configurations, click plus, and select Cargo Command.

    2. Specify the settings for your Cargo Command configuration:

      Cargo run/debug configuration

      • Use the following pattern for the Command field: [run] or [test] [build options] [--] [program arguments].

        Notice the -- prefix followed by an extra space. It separates the input arguments from the build options that will be passed to Cargo. For example, when you debug this configuration, the plugin will start by calling cargo build [build options] and then it will launch the binary under the debugger with [program arguments].

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

      • Backtrace lets you set the RUST_BACKTRACE environment variable, which prints the unwound stack trace into error messages and controls its length. For more information, see Debugging Rust code in CLion: More debug features.

    3. After saving the configuration, you can use it to run or debug your Rust application.

    Testing

    When you run the cargo test command or use a gutter icon to run tests, 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 App run configurations test state run the tests or toggle automatic rerun on changes in your code, sort the tests alphabetically App object browser sorted or by duration App run configurations sortby duration, and open previous results App vcs history:

    Test runner

    Debugging

    If required, you can configure the debugger data views in Settings | 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.

    After the configuration is set up, you can DebugApp actions start debugger 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 App toolwindows tool window profiler to profile your application or App actions start memory profile 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: 11 January 2023