Working with Rust: Formatting, Linting & Auto-completion

Reading Time: 4 minutes

Every language has its own toolset for writing effective and clean code, Rust is no different. So in this blog, we would be describing how to format the rust code, while working with different editors or IDE.

Formatting code is a mechanical task which takes both time and mental effort. By using an automatic formatting tool, a programmer is relieved of this task and can concentrate on more important things. Furthermore, by sticking to an established style guide, programmers don’t need to formulate ad hoc style rules. Having consistent, standardized automated code styling in a project can be useful for code review, attracting new contributors, and avoiding unproductive discussions.

So here, we will be discussing the following scenarios:

  1. Using default text-editors.
  2. Using Sublime.
  3. Using Atom.
  4. Using IDE (like IntelliJ or Eclipse).

Prerequisite: We are considering that rust and cargo are already installed in your system. Please follow the blog RUST: Quick Start & Exploring Cargo to install rust and cargo if you haven’t got a chance to install them.

Scenario 1: Using a default text editor

While using a default editor, it’s difficult for developers to work on business logic and actual code while keeping an eye on the coding style.  So for making the developer’s job easier, we can use the following tools:

  1. RustFmt: 

    Rustfmt is a tool for formatting Rust source code. You can install it using Cargo:

    $ cargo install rustfmt

    This will install an executable into ~/.cargo/bin/. In order to use it, add it into your $PATH variable. In case you are using Bash, follow these steps:

    $ echo ‘export PATH=$PATH:$HOME/.cargo/bin’ >> $HOME/.bashrc $ source $HOME/.bashrc

    Now you can use rustfmt to format either a single file or the whole project:
    Single file:

        $ rustfmt src/main.rs

    Project:

        $ cargo fmt

    Note 1: If Rustfmt crashes during formatting, please get a backtrace by re-running with

    RUST_BACKTRACE=1

    Note 2: Some code will need fixing up after Rustfmt is done. There are two reasons for this: in some places, Rustfmt won’t reformat the code yet, but it moves around surrounding code in a way which makes this a problem. In other places you might want non-standard formatting, for example, if you have a 3×3 array which represents a matrix, you might want this on three lines even though rustfmt can fit it on one line.
    Note 3: you can use the #[rustfmt_skip] attribute. This can be placed on functions, modules, and most other items. Again, after fixing up the source code and adding the attribute, check that Rustfmt does not make any further changes.
    Note 4:At this point we can run cargo fmt to format a repository. It runs rustfmt in the ‘replace’ mode which creates backup files with a .bk extension. If our project is already in version control we may not want this. If that is the case we can edit rustfmt.toml to include the following:

    $ write_mode = “overwrite”

    Note 5: The rustfmt.toml lets us configure the various options found in

    $ rustfmt –config-help.

  2. Clippy

    The Rust compiler is already quite strict, clippy goes a step further and helps prevent some things which are valid but bad practice. This can help prevent unexpected problems later in your code.
    Clippy requires nightly in order to run. If it’s not installed please install that first. Once nightly is installed, we could use the following command:

    $ cargo +nightly install clippy

    Now we can run clippy with the following command:

    $ cargo +nightly clippy

  3. Racer:

    Racer saves us some time looking through documentation and helps us discover functionality as we type. Racer is a code completion utility that’s used in various editor addons. First, install it:

    $ cargo install racer

    Next we need to set an environment variable so racer knows where to look for the Rust source. In your ~/.bashrc (or ~/.zshrc etc) add the following line:
    # Mac

    export RUST_SRC_PATH=${HOME}/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/src

    # Linux

    export RUST_SRC_PATH=${HOME}/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src

    Then you can test that racer works by doing a test run via the command line:

    $ racer complete std::io::B

Scenario 2: Using Sublime as the editor

While using sublime editor, we have following steps to be followed, to get the maximum out of it:

Step 1: Go to View, move to Syntax and select Rust.

Step 2: To select the build system, go to Tools, hover on to the Build System and select Rust.

Step 3: Setup Anaconda_Rust package: Anaconda Rust offers auto completion, auto formatting and linting for Rust language that will never freeze your Sublime Text 3.
Follow the steps in the link, to add the package.

And now you are ready to use Sublime as your regular rust programming editor.

Scenario 3: Using Atom as the editor

Atom has a lot of packages to look into and here are the few that could be used:

– rustfmt
– racer
– language-rust
– linter-rust
– rust-api-docs-helper
– languageserver-rust
– build-cargo
– autocomplete-crates

Go through them and you will feel blessed to have these packages along your side while working with Atom to write Rust codes.

Scenario 4: Using IntelliJ IDE

IntelliJ provides a Rust plugin that would ease a developer’s life while working on Rust. Like for other languages, we get the same help in IntelliJ for rust, syntax highlighting, code formatting, code selection, code completion, source code navigation etc. For detailed features please visit the link.

Hope this blog would be helpful for you while choosing your editor for Rust programming and using the power of the editors’ packages and plugins.

For more blogs regarding Rust, keep going through our blogs, because we at Knoldus believe in working on the cutting-edge technologies and help out the community of tech-enthusiasts by sharing our experiences and knowledge.

knoldus-advt-sticker

Written by 

Anmol Mehta is a Software Consultant having experience of more than 1.5 years. A keen programmer who has experience in Scala and Java. He is recognized as a dedicated and determined team player who enjoys working on new technologies. He is a professional and a technology enthusiast. He believes in the approach that “teamwork makes the dream work”. He is a quick and always-on learner who ensures better quality in every increment of his work. He believes in abiding standard coding practices. He always looks after that the team is working in sync with each other both at the technical and managerial level.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading