Format Rust code with cargo format

Jim Bennett | Jan 26, 2023

This blog post was a fun one to write - cos I didn’t! I actually learned about cargo fmt on a recent Crack the code interview with Rust live stream and decided a quick blog post would be fun.

So I launched ChatGPT and asked it to write this post! This is one of 2 posts written this way, so you will notice how similar this is to my cargo clippy post.

Rust is a popular programming language known for its safety and performance. One of the great things about Rust is the abundance of tools available to make development easier and more efficient. One such tool is cargo fmt, which is a command line utility for formatting Rust code. In this blog post, we’ll show you how to use cargo fmt to make your Rust code more readable and consistent.

First, make sure that you have Rust and Cargo installed on your machine. If you don’t, you can download and install them from the official Rust website. Once you have Rust and Cargo set up, you can use the cargo fmt command to format your code.

To format a specific file, navigate to the root directory of your project in the command line and run the following command:

cargo fmt --file=path/to/file.rs

This will format the file located at path/to/file.rs.

If you want to format all the files in your project, navigate to the root directory of your project in the command line and run the following command:

cargo fmt

This will format all the files in your project.

You can also format all the files in your project and check for formatting errors by running the following command:

cargo fmt --check

This will check for formatting errors and display them in the command line. If there are no errors, the command will exit with a 0 status code, otherwise it will exit with a non-zero status code.

In addition to formatting files, cargo fmt also provides options to configure the formatting behavior. For example, you can configure the number of spaces used for indentation or the maximum line width. You can find more information about these options in the official Cargo documentation.

In conclusion, cargo fmt is a powerful tool that can help you to make your Rust code more readable and consistent. It’s easy to use and provides a lot of options to customize the formatting behavior. By using cargo fmt, you can spend less time worrying about formatting and more time focusing on writing great code.