In this video, we'll discuss working
effectively with Cargo fmt and Cargo
clippy. Cargo fmt is a really useful
tool for Auto formatting your Rust code
according to Rust style guidelines. If
you get an error when you run Cargo
fmt, then you need to install the Rust
format component using Rust up, it looks
like this.
Rust fmt is most likely already
installed so you probably won't have to
run that step. Once its installed
Cargo fmt will do its job, Cargo
fmt is usually silent what it does is
auto format your code. Let's go take a
look here's our code, and it's already
properly formatted, so let's mess it up a
bit.
Okay that's a bit of an odd way to
format things, let's go and run Cargo
fmt and see what happens.
Now I'll go back to our code
again and we'll see that Cargo fmt
moved it back to the correct format. This is
super useful to avoid arguments about
where parens go, wear braces go, how you
should format things. There's one
suggested way to do it, there's one
built-in tool to do it, if you just use
it no one has to have any arguments,
it's just however the tool does it, and
it looks pretty good.
Okay what about Cargo clippy? This might
also already be installed on your system
but if it isn't just run Rust up
component add clippy, and Rust up will
install it for you. While Cargo fmt
deals with style,
clippy is a collection of lints to catch
common mistakes and help you improve
your Rust code. Let's try it out
let's start by looking at the second
warning use of unwrapped or followed by
a call to new. So the problem here is
that string new will happen, whether or
not the value is needed. So we're going
to do an allocation and create a new
string, whether or not we actually need
the new string. So this is suggesting
instead call unwrap or default, and then
inside the method it will create a
default for a string, which is a blank
string only if it's needed.
So that's an optimization, so let's go
fix that.
OK we've made the fix, let's go run
Cargill clippy again.
All right we fixed that item as we can
see it doesn't show up anymore, let's
look at the next one. Length comparison
to zero so we're using dot Len to get a
length, and we're comparing it to zero
and we're told using naught is empty is
clearer and more explicit. Okay
so they want us to put a not in front,
and use is empty instead of Len, so let's
go make that change.
Alright let's go around Cargo clippy
again,
great clippy couldn't find anything to
complain about. I suggest using Cargo
fmt and Cargo clippy, either in a
continuous integration check, or put it
in something like a Git pre-commit hook.
I'll give an example of using this in a
Git pre-commit hook. The file to create
is dot Git hooks pre-commit.
I'm going to paste in my preferred setup.
This file will be interpreted as
Shell code. This will make it so that
before get makes a commit it will run
Cargo fmt, and then let it do its
thing and then it will run Cargo clippy
and abort the commit if any warnings are
present. The - D warnings turns the
warnings into errors, which will cause
the hook to fail. We can go see it in
action if I go add a formatting problem
and do something that will make clippy
complain, but first let's save this,
and let's make it executable.
And now let's go at our changes.
Here is a change that Cargo fmt will
revert,
and here's some code that clippy will
complain about. Let's try to commit our
code and see what happens.
Okay so clippy complained about the
linting error, what about Cargo fmt? If
we go back we see Cargo fmt did its
thing, so then we fix the linting error,
and now when we try to commit it should
work.
In the next video, we'll talk about how
to handle errors gracefully.