News

Rust plugin update: language support improvements, new quick-fixes, improved debugger and more

Hi,

CLion 2018.2 was released just recently. Along with the release, the Rust plugin has had an update! In this blog post, we’d like to highlight some of the main improvements in the plugin.

However, before we do that, let me briefly remind you about the current state of the Rust plugin:

  • It’s available in all our IntelliJ-based IDEs.
  • It relies on the Cargo build system, and CLion is no exception here!
  • The debugging feature for Rust code is currently only available in CLion.

Here we listed earlier the features in the Rust plugin. What’s new for Rust with the 2018.2 update?

Language support

There is a set of language support improvements. We’ll highlight the most important:

  • Pattern-matching on references got some attention in the language (RFC 2005), and now it is supported properly in the IntelliJ Rust plugin, which means the types are inferred correctly.
  • The expansion of declarative macros guarantees that you can now get better code insight when using macros. For example, the completion works:
    Macro Expansion
    The best use of it is for error handling via error-chain crate based on macros.

    Keep in mind its limitations – if the impl block is generated with the macro on its own, then such completion won’t work as the IDE currently is not aware of its expansion.

New and improved quick-fixes

A whole new set of quick-fixes were implemented to make your Rust code more accurate and save you time while coding. The first group of quick-fixes is related to the type-check errors. For example, when a manual cast is missing in the code (as an implicit cast is not allowed for primitive types) you can generate a proper cast with Alt+Enter: Numeric type cast

As CLion (and the other IntelliJ-based IDEs with the Rust plugin) is aware of the traits available for the type, it can suggest type conversions to fix type checking errors in the code using the proper trait. An example of how this could be used is if a string was used by mistake, but the FromStr trait is present for the type, then a quick-fix invoked with Alt+Enter can generate a proper conversion:
String trait usage

By the way, while we are on the topic of strings, as you know String and &str are not the same in Rust. The main difference is in the object ownership – &str is just a reference to some location in the memory, it doesn’t own it. So what if a &str is passed where the String is needed? There are several options actually, and all of them are available in the quick-fix menu:
String convertion

You can also convert a struct to reference to this struct where needed:
Reference quick-fix

There are even more quick-fixes available for the type checks, thanks to oleg-semenov’s incredible contribution to the IntelliJ Rust plugin!

As for the other code analysis checks, an unresolved reference check with the Auto Import quick-fix can save you time if you have tried to start using something before importing it:
Unresolved reference

Besides, when you implement missing members via a quick-fix, the IntelliJ Rust plugin will correctly substitute a generic type parameter and lifetimes for you! How cool is that?Implement missing members

Quick documentation update

Quick Documentation (Ctrl+Q on Linux/Windows, F1/^J on macOS) shows the documentation for the symbol at the caret. And now it includes the links to https://doc.rust-lang.org/std/ for the standard library items:
Quick Documentation

Pretty-printers in debugger

CLion comes with the Rust debugger included. You know that! And now it loads the default Rust pretty-printers for you, so that you now have a nice view of the values of the default types like strings or vectors.

Better project view

The Cargo tool window (View | Toolwindows | Cargo) now shows all the workspace members and targets:
Cargo toolwindow
It simplifies the way to run targets – you can now simply click on them in this tool window. You can also refresh your Cargo project, run clippy or a Cargo command, and attach or detach a Cargo project to the workspace.

There are some important enhancements to the editor’s performance, so that the typing in Rust feels smoother and the editor is more responsive. And there is even more! Check out the collection of recent release notes here.

Wanna give new features a try? Get CLion (or any other IntelliJ-based IDE) with the Rust plugin now!

Download CLion

Your Rust Team

JetBrains
The Drive to Develop

image description

Discover more