Unused mut performance

does unused mut affect performance?

Probably not. Code that has no side effects is eliminated by the optimizer (for release builds).

But I suspect you’ve excluded pertinent details. Why does your code have unused mutual references?

I just havent finished writing the code that uses mut variables yet. I see a lot of warnings and I have this question.

If you have not finished writing the code, then you should not be worrying about performance. It’s no good if the code is fast if it doesn’t do anything useful.

If you are at the point where you need to be studying performance, you should be cleaning up the warnings, because some warnings are relevant to whether your code works well.

If the code with the warnings isn’t relevant to the code that you need to measure the performance of, then you are tackling too much of the problem at once. Adding #![allow(unused)] temporarily to the unfinished module might be a suitable temporary fix, but be sure to remove it later.

7 Likes

You can think of mut as a marker trait that limit and prevent illegal mutations that may occure accidentally or unknowingly, and to be assured, the compiler removes the mut keyword from your code before reaching the LLVM(it handles optimisations and code generation in short).