David's Blog

Using Rustfmt to Format Rust Code

By David Li on 2023-04-20T14:45:32.893Z

Using Rustfmt to Format Rust Code

Rustfmt is a code formatting tool for the Rust programming language. It helps developers maintain a consistent coding style across their projects, making the codebase more readable and maintainable. This article will guide you through the process of installing Rustfmt, configuring it, and using it to format your Rust code.

Table of Contents

  1. Installing Rustfmt
  2. Configuration
  3. Using Rustfmt
  4. Integrating with IDEs and Editors
  5. Conclusion

1. Installing Rustfmt

Rustfmt is part of the Rust toolchain, so if you have Rust installed, you likely already have Rustfmt. To check if Rustfmt is installed, run the following command:

rustup component list | grep rustfmt

If you see rustfmt as an installed component, you’re good to go. If not, install it by running:

rustup component add rustfmt

2. Configuration

Rustfmt offers several configuration options to customize the formatting style. To configure Rustfmt, create a rustfmt.toml or .rustfmt.toml file in the root of your project.

Here’s a sample configuration file with some commonly used options:

edition = "2021"
max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
use_small_heuristics = "Default"
  • edition: Specifies the Rust edition to use, e.g., “2015”, “2018”, or “2021”.
  • max_width: The maximum line width for your code. Default is 100 characters.
  • hard_tabs: Use hard tabs instead of spaces for indentation. Default is false.
  • tab_spaces: The number of spaces per indentation level. Default is 4 spaces.
  • newline_style: The line ending style to use. Options are “Auto”, “Native”, “Unix”, and “Windows”.
  • use_small_heuristics: Determines the heuristic used for fitting items within the max_width. Options are “Default”, “Max”, “Off”, and “Minimum”.

For a complete list of configuration options, refer to the official Rustfmt configuration documentation.

3. Using Rustfmt

To format a single Rust file, run the following command:

rustfmt path/to/your_file.rs

To format all Rust files in your project, run the following command from your project’s root directory:

cargo fmt

You can also check if your code is formatted correctly without actually modifying the files by running:

cargo fmt -- --check

This command will return a non-zero exit code if any file requires formatting.

4. Integrating with IDEs and Editors

Most modern IDEs and text editors support Rustfmt integration. Here are a few examples:

  • VSCode: Install the Rust extension, which includes Rustfmt support. You can enable the “Format on Save” option to automatically format your Rust code when you save a file.
  • IntelliJ IDEA: Install the IntelliJ Rust plugin. Rustfmt integration is built-in, and you can configure it in the Rust settings page.
  • Sublime Text: Install the Rust Enhanced package, which includes Rustfmt support. You can configure it to run on save or manually through the command palette.
  • Vim: Install the rust.vim plugin, which provides Rustfmt integration. Configure it to run on save, or use the :RustFmt command to format the current file.

5. Conclusion

Using Rustfmt allows you to maintain a consistent coding style in your Rust projects, improving code readability and maintainability. By configuring Rustfmt to your preferred style and integrating it into your development environment, you can ensure that your code is always formatted properly.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-04-15.