1. 50
    Rustlings clippy1: Using Clippy linting to discover potentially dangerous code scenarios
    1m 46s

Rustlings clippy1: Using Clippy linting to discover potentially dangerous code scenarios

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

README for this exercise.

Chris Biscardi: [0:00] In clippy1, we see the first set of Rust code that will trigger the clippy linter. The Clippy tool is a collection of lints in addition to what the rust compiler already tells us. In this case, we can see that the Rust compiler with the clippy lints added is warning us about a strict comparison of 'f32' or 'f64'.

[0:21] The Help message here tells us that we should consider comparing them with some kind of error. To learn more about this particular error, you can click on this link in the Help text. What this means for us is that when we compare these two floating point numbers, we want to do it within an error boundary because two floating point numbers can have very small differences when we operate on them.

[0:41] Doing exactly quality means that we could have a difference of .0000000000001 which might not matter to us but will indicate in our program that these two values are not exactly equal. What we are going to do is define a number -- typically, called an epsilon -- that we care about. In this case, I'm going to start with 0001.

[1:05] You can see that these numbers are both 1.233, and then they differ at that point. We're going to say that that number is the number we care about. Instead of doing directly quality, what we're going to do is do x - y, get the absolute value of that, and then check to see that it's below the error boundary.

[1:23] Note that this passes clippy's requirements. We're not quite done yet. If we rustlings run clippy1, we can see that we got a Success!, so they're effectively the same number. If we cared to be more precise, we could add a zero to our error and run this program again.

[1:37] Note that the same code that is more precise also satisfies clippy, but now, these two numbers are not considered equal because we care about a higher precision than we did before.