- Recreating the Issue
- Fixing the Issue
You get a warning (IDE or Cargo) that tells you to consider using a
to fix your error… then you notice that the line in question is already
let. What’s going on?
Recreating the Issue
First, let’s setup a simple situation that recreates the issue.
Loading some Fake Data
First, we’ll load some fake data.
The contents of this function don’t matter so much; but make the sample easier to illustrate.
So we write a main function to grab that data, unwrap it and just print out the first key/value pairs.
Note that we are using the
nightly toolchain to enable the
Here, we see our first confusing suggestion.
It’s telling us to consider using a
let on the line that is already using a
What does cargo tell us? Maybe it’s better?
Cargo is a bit more descriptive about why it is a problem. It seems the value is dropped
at the end of the
unwrap call. Further down though, we can see a confusing suggestion
let. Well, let’s just try it.
Trying the suggested
As expected, delegating it to another variable does not actually impact the error. I am not sure why cargo and the IDE are suggesting it.
Fixing the Issue
If the cargo suggestion does not work, how do we fix it?
If we think about the fact that both cargo and the IDE both suggested introducing a
statement, we might see that it is still possible.
In this example, we make the function call on one line, then add an
if let on the next line.
By replacing the
if let, the error goes away.
The app will now run. Sometimes it will print the first key/value pair and sometimes, because of our chaos, it will say that it failed to load any data.