Adding cargo fmt blog post

This commit is contained in:
Jim Bennett
2023-01-27 01:09:41 +00:00
parent a1bddc3640
commit 49f7911d25
3 changed files with 59 additions and 7 deletions

View File

@@ -309,13 +309,13 @@ params:
livestreams:
upcoming:
series:
- title: "Crack code interview problems in Rust"
videos:
- title: "Episode 3"
description: "Come watch experts try (and possibly fail) to solve coding challenges in Rust!"
registrationLink: https://developer.microsoft.com/reactor/events/17774/
image: https://img.youtube.com/vi/ugz1YgoZmzI/0.jpg
# series:
# - title: "Crack code interview problems in Rust"
# videos:
# - title: "Episode 3"
# description: "Come watch experts try (and possibly fail) to solve coding challenges in Rust!"
# registrationLink: https://developer.microsoft.com/reactor/events/17774/
# image: https://img.youtube.com/vi/ugz1YgoZmzI/0.jpg
previous:
series:
- title: "Crack code interview problems in Rust"
@@ -326,6 +326,9 @@ params:
- title: "Episode 2 - Conway's game of life"
id: 0jKSfKqOoFc
description: "Come watch experts try (and possibly fail) to solve coding challenges in Rust! In this episode, Jim is joined by Ryan Levick from the Rust Foundation board and together they try to implement Conway's game of life."
- title: "Episode 3 - Conway's game of life continues"
id: 2Dd6wACDna8
description: "Come watch experts try (and possibly fail) to solve coding challenges in Rust! In this episode, Jim is joined by Yosh Wuyrt, a Rust developer advocate and together they try to finish the implementation of Conway's game of life from last week."
- title: "Tech Career Tips - Sea of Nerds"
videos:
- title: "Tech career tips from educators at Microsoft Sea of Nerds"

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

View File

@@ -0,0 +1,49 @@
---
author: "Jim Bennett"
date: 2023-01-26
description: "Learn how to quickly format Rust code with cargo format"
draft: false
tags: ["rust", "cargo", "fmt", "format", "code","writtenbyai"]
title: "Format Rust code with cargo format"
images:
- /blogs/cargo-fmt/banner.png
featured_image: banner.png
---
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:
```bash
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:
```bash
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:
```bash
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.
> 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](https://www.youtube.com/watch?v=2Dd6wACDna8) and decided a quick blog post would be fun.
>
> So I launched [ChatGPT](https://chat.openai.com/chat) and asked it to write this post!